home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / SinfoSeguros / NET / MDAC_TYP.EXE / sqlnet.cab / instcat.sql < prev    next >
Encoding:
Text File  |  2003-02-06  |  934.6 KB  |  24,574 lines

  1. /*
  2. **  INSTCAT.SQL
  3. **  Installs catalog stored procedures on the Microsoft SQL Server.
  4. ** Copyright Microsoft, Inc. 1994 - 2000
  5. ** All Rights Reserved.
  6. */
  7.  
  8. /*
  9. INTERNAL NOTE:
  10. When this file is updated, you MUST change the last row inserted into
  11. spt_server_info to be version number of this file.  the convention is
  12. j.nn.bbb, where j is the major version number ('7' now), nn is the minor
  13. version number ('10' now), and bbb is the build number.
  14. insert into spt_server_info
  15.     values (500, 'SYS_SPROC_VERSION', '8.00.bbb')
  16. */
  17.  
  18. /****************************************************************************/
  19. /* This portion sets up the ability to perform all the functions in this    */
  20. /* script                                                                   */
  21. /****************************************************************************/
  22. go
  23. use master
  24. go
  25. dump tran master with no_log
  26. go
  27.  
  28. set quoted_identifier on
  29. go
  30.  
  31. if (exists (select * from sysobjects
  32.         where name = 'sp_configure' and type = 'P '))
  33. begin
  34.     execute sp_configure 'update',1
  35. end
  36. reconfigure with override
  37. go
  38.  
  39. exec sp_MS_upd_sysobj_category 1 /*Capture datetime for use below.*/
  40.  
  41. go
  42.  
  43. /*
  44. ** If old versions of tables exist, drop them.
  45. */
  46. if (exists (select * from sysobjects
  47.         where name = 'MSdatatype_info' and type = 'U '))
  48.     drop table MSdatatype_info
  49. go
  50. if (exists (select * from sysobjects
  51.         where name = 'MSdatatype_info_ext' and type = 'U '))
  52.     drop table MSdatatype_info_ext
  53. go
  54. if (exists (select * from sysobjects
  55.         where name = 'MStable_types' and type = 'U '))
  56.     drop table MStable_types
  57. go
  58. if (exists (select * from sysobjects
  59.         where name = 'MSserver_info' and type = 'U '))
  60.     drop table MSserver_info
  61. go
  62. if (exists (select * from sysobjects
  63.         where name = 'spt_table_types' and type = 'U '))
  64.     drop table spt_table_types
  65. go
  66. /*
  67. ** If tables or procs already exist, drop them.
  68. */
  69.  
  70. if (exists (select * from sysobjects
  71.         where name = 'spt_datatype_info' and type = 'U '))
  72.     drop table spt_datatype_info
  73. go
  74. if (exists (select * from sysobjects
  75.         where name = 'spt_datatype_info_ext' and type = 'U '))
  76.     drop table spt_datatype_info_ext
  77. go
  78. if (exists (select * from sysobjects
  79.         where name = 'sp_add_server_sortinfo' and type = 'P '))
  80.     drop proc sp_add_server_sortinfo
  81. go
  82. if (exists (select * from sysobjects
  83.         where name = 'sp_add_server_sortinfo75' and type = 'P '))
  84.     drop proc sp_add_server_sortinfo75
  85. go
  86. if (exists (select * from sysobjects
  87.         where name = 'spt_server_info' and type = 'U '))
  88.     drop table spt_server_info
  89. go
  90. if (exists (select * from sysobjects
  91.         where name = 'sp_tables' and type = 'P '))
  92.     drop proc sp_tables
  93. go
  94. if (exists (select * from sysobjects
  95.         where name = 'sp_statistics' and type = 'P '))
  96.     drop proc sp_statistics
  97. go
  98. if (exists (select * from sysobjects
  99.         where name = 'sp_columns' and type = 'P '))
  100.     drop proc sp_columns
  101. go
  102. if (exists (select * from sysobjects
  103.         where name = 'sp_fkeys' and type = 'P '))
  104.     drop proc sp_fkeys
  105. go
  106. if (exists (select * from sysobjects
  107.         where name = 'sp_pkeys' and type = 'P '))
  108.     drop proc sp_pkeys
  109. dump tran master with no_log
  110. go
  111.  
  112. go
  113. if (exists (select * from sysobjects
  114.         where name = 'sp_stored_procedures' and type = 'P '))
  115.     drop proc sp_stored_procedures
  116. go
  117. if (exists (select * from sysobjects
  118.         where name = 'sp_sproc_columns' and type = 'P '))
  119.     drop proc sp_sproc_columns
  120. go
  121. if (exists (select * from sysobjects
  122.         where name = 'sp_table_privileges' and type = 'P '))
  123.     drop proc sp_table_privileges
  124. go
  125. if (exists (select * from sysobjects
  126.         where name = 'sp_column_privileges' and type = 'P '))
  127.     drop proc sp_column_privileges
  128. go
  129. dump tran master with no_log
  130. go
  131. if (exists (select * from sysobjects
  132.         where name = 'sp_server_info' and type = 'P '))
  133.     drop proc sp_server_info
  134. go
  135. if (exists (select * from sysobjects
  136.         where name = 'sp_datatype_info' and type = 'P '))
  137.     drop proc sp_datatype_info
  138. go
  139. if (exists (select * from sysobjects
  140.         where name = 'sp_special_columns' and type = 'P '))
  141.     drop proc sp_special_columns
  142. go
  143. if (exists (select * from sysobjects
  144.         where name = 'sp_databases' and type = 'P '))
  145.     drop proc sp_databases
  146. go
  147. dump tran master with no_log
  148. go
  149. if (exists (select * from sysobjects
  150.         where name = 'sp_ddopen' and type = 'P '))
  151.     drop proc sp_ddopen
  152. go
  153. if (exists (select * from sysobjects
  154.         where name = 'sp_tableswc' and type = 'P '))
  155.     drop proc sp_tableswc
  156. go
  157.  
  158. if (exists (select * from sysobjects
  159.         where name = 'sp_tablecollations' and type = 'P'))
  160.     drop proc sp_tablecollations
  161. go
  162.  
  163. if (exists (select * from sysobjects
  164.         where name = 'sp_bcp_dbcmptlevel' and type = 'P'))
  165.     drop proc sp_bcp_dbcmptlevel
  166. go
  167.  
  168. dump tran master with no_log
  169. go
  170.  
  171.  
  172. if (exists (select * from sysobjects
  173.         where name = 'spt_provider_types' and type = 'U '))
  174.     begin
  175.     drop table spt_provider_types
  176.     dump tran master with no_log
  177.     end
  178. go
  179. if (exists (select * from sysobjects
  180.         where name = 'sp_catalogs_rowset' and type = 'P '))
  181.     begin
  182.     drop procedure sp_catalogs_rowset
  183.     dump tran master with no_log
  184.     end
  185. go
  186. if (exists (select * from sysobjects
  187.         where name = 'sp_column_privileges_rowset' and type = 'P '))
  188.     begin
  189.     drop procedure sp_column_privileges_rowset
  190.     dump tran master with no_log
  191.     end
  192. go
  193. if (exists (select * from sysobjects
  194.         where name = 'sp_columns_rowset' and type = 'P '))
  195.     begin
  196.     drop procedure sp_columns_rowset
  197.     dump tran master with no_log
  198.     end
  199. go
  200. if (exists (select * from sysobjects
  201.         where name = 'sp_check_constraints_rowset' and type = 'P '))
  202.     begin
  203.     drop procedure sp_check_constraints_rowset
  204.     dump tran master with no_log
  205.     end
  206. go
  207. if (exists (select * from sysobjects
  208.         where name = 'sp_check_constbytable_rowset' and type = 'P '))
  209.     begin
  210.     drop procedure sp_check_constbytable_rowset
  211.     dump tran master with no_log
  212.     end
  213. go
  214. if (exists (select * from sysobjects
  215.         where name = 'sp_foreign_keys_rowset' and type = 'P '))
  216.     begin
  217.     drop procedure sp_foreign_keys_rowset
  218.     dump tran master with no_log
  219.     end
  220. go
  221. if (exists (select * from sysobjects
  222.         where name = 'sp_indexes_rowset' and type = 'P '))
  223.     begin
  224.     drop procedure sp_indexes_rowset
  225.     dump tran master with no_log
  226.     end
  227. go
  228. if (exists (select * from sysobjects
  229.         where name = 'sp_primary_keys_rowset' and type = 'P '))
  230.     begin
  231.     drop procedure sp_primary_keys_rowset
  232.     dump tran master with no_log
  233.     end
  234. go
  235. if (exists (select * from sysobjects
  236.         where name = 'sp_procedure_params_rowset' and type = 'P '))
  237.     begin
  238.     drop procedure sp_procedure_params_rowset
  239.     dump tran master with no_log
  240.     end
  241. go
  242. if (exists (select * from sysobjects
  243.         where name = 'sp_procedures_rowset' and type = 'P '))
  244.     begin
  245.     drop procedure sp_procedures_rowset
  246.     dump tran master with no_log
  247.     end
  248. go
  249. if (exists (select * from sysobjects
  250.         where name = 'sp_provider_types_rowset' and type = 'P '))
  251.     begin
  252.     drop procedure sp_provider_types_rowset
  253.     dump tran master with no_log
  254.     end
  255. go
  256. if (exists (select * from sysobjects
  257.         where name = 'sp_schemata_rowset' and type = 'P '))
  258.     begin
  259.     drop procedure sp_schemata_rowset
  260.     dump tran master with no_log
  261.     end
  262. go
  263. if (exists (select * from sysobjects
  264.         where name = 'sp_statistics_rowset' and type = 'P '))
  265.     begin
  266.     drop procedure sp_statistics_rowset
  267.     dump tran master with no_log
  268.     end
  269. go
  270. if (exists (select * from sysobjects
  271.         where name = 'sp_tables_rowset' and type = 'P '))
  272.     begin
  273.     drop procedure sp_tables_rowset
  274.     dump tran master with no_log
  275.     end
  276. go
  277. if (exists (select * from sysobjects
  278.         where name = 'sp_tables_info_rowset' and type = 'P '))
  279.     begin
  280.     drop procedure sp_tables_info_rowset
  281.     dump tran master with no_log
  282.     end
  283. go
  284. if (exists (select * from sysobjects
  285.         where name = 'sp_tables_info_rowset_64' and type = 'P '))
  286.     begin
  287.     drop procedure sp_tables_info_rowset_64
  288.     dump tran master with no_log
  289.     end
  290. go
  291. if (exists (select * from sysobjects
  292.         where name = 'sp_table_constraints_rowset' and type = 'P '))
  293.     begin
  294.     drop proc sp_table_constraints_rowset
  295.     dump tran master with no_log
  296.     end
  297. go
  298. if (exists (select * from sysobjects
  299.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  300.     begin
  301.     drop proc sp_table_privileges_rowset
  302.     dump tran master with no_log
  303.     end
  304. go
  305. if (exists (select * from sysobjects
  306.         where name = 'sp_linkedservers_rowset' and type = 'P '))
  307.     begin
  308.     drop proc sp_linkedservers_rowset
  309.     dump tran master with no_log
  310.     end
  311. go
  312.  
  313. if (exists (select * from sysobjects
  314.         where name = 'sp_table_statistics_rowset' and type = 'P '))
  315.     begin
  316.     drop proc sp_table_statistics_rowset
  317.     dump tran master with no_log
  318.     end
  319. go
  320.  
  321. if (exists (select * from sysobjects
  322.         where name = 'sp_oledb_column_constraints' and type = 'P '))
  323.     begin
  324.     drop proc sp_oledb_column_constraints
  325.     dump tran master with no_log
  326.     end
  327. go
  328.  
  329. if (exists (select * from sysobjects
  330.         where name = 'sp_oledb_indexinfo' and type = 'P '))
  331.     begin
  332.     drop proc sp_oledb_indexinfo
  333.     dump tran master with no_log
  334.     end
  335. go
  336.  
  337. if (exists (select * from sysobjects
  338.         where name = 'sp_oledb_ro_usrname' and type = 'P '))
  339.     begin
  340.     drop proc sp_oledb_ro_usrname
  341.     dump tran master with no_log
  342.     end
  343. go
  344.  
  345. if (exists (select * from sysobjects
  346.         where name = 'sp_oledb_deflang' and type = 'P '))
  347.     begin
  348.     drop proc sp_oledb_deflang
  349.     dump tran master with no_log
  350.     end
  351. go
  352.  
  353. if (exists (select * from sysobjects
  354.         where name = 'sp_oledb_defdb' and type = 'P '))
  355.     begin
  356.     drop proc sp_oledb_defdb
  357.     dump tran master with no_log
  358.     end
  359. go
  360.  
  361. if (exists (select * from sysobjects
  362.         where name = 'sp_oledb_database' and type = 'P '))
  363.     begin
  364.     drop proc sp_oledb_database
  365.     dump tran master with no_log
  366.     end
  367. go
  368.  
  369. if (exists (select * from sysobjects
  370.         where name = 'sp_oledb_language' and type = 'P '))
  371.     begin
  372.     drop proc sp_oledb_language
  373.     dump tran master with no_log
  374.     end
  375. go
  376.  
  377. print 'creating table spt_datatype_info_ext'
  378. go
  379.  
  380. if (charindex('6.00', @@version) = 0 and
  381.     charindex('6.50', @@version) = 0 and
  382.     charindex('7.00', @@version) = 0 and
  383.     charindex('8.00', @@version) = 0)
  384. begin   /*  Pre 6.0 server */
  385.     print ''
  386.     print ''
  387.     print 'Warning:'
  388.     print 'you are installing the stored procedures '
  389.     print 'on a pre 6.0 SQL Server.'
  390.     print 'Ignore the following error.'
  391.     create table spt_datatype_info_ext (
  392.                 user_type       smallint    not null,
  393.                 CREATE_PARAMS   varchar(32) null,
  394.                 AUTO_INCREMENT  smallint null,
  395.                 typename        varchar(32))
  396. end
  397. go
  398. if (charindex('6.00', @@version) > 0 or
  399.     charindex('6.50', @@version) > 0 or
  400.     charindex('7.00', @@version) > 0 or
  401.     charindex('8.00', @@version) > 0)
  402. begin   /* 6.0 or later server */
  403.     create table spt_datatype_info_ext (
  404.                 user_type       smallint    not null,
  405.                 CREATE_PARAMS   varchar(32) null,
  406.                 AUTO_INCREMENT  smallint null,
  407.                 typename        sysname)    /* from systypes, to avoid xusertype hard-code */
  408. end
  409. go
  410.  
  411. grant select on spt_datatype_info_ext to public
  412. go
  413.  
  414.  
  415. insert into spt_datatype_info_ext
  416.     /* CHAR      user_type, create_params, auto_increment */
  417.     values           (1,    'length' ,0, 'char')
  418.  
  419. insert into spt_datatype_info_ext
  420.     /* VARCHAR   user_type, create_params, auto_increment */
  421.     values           (2,    'max length' ,0, 'varchar')
  422.  
  423. insert into spt_datatype_info_ext
  424.     /* BINARY    user_type, create_params, auto_increment */
  425.     values           (3,    'length' ,0, 'binary')
  426.  
  427. insert into spt_datatype_info_ext
  428.     /* VARBINARY user_type, create_params, auto_increment */
  429.     values           (4,    'max length' ,0, 'varbinary')
  430.  
  431. if  (charindex('6.00', @@version) > 0 or
  432.      charindex('6.50', @@version) > 0 or
  433.      charindex('7.00', @@version) > 0 or
  434.      charindex('8.00', @@version) > 0)
  435. begin   /*  Add 6.0 data types */
  436.     insert into spt_datatype_info_ext
  437.         /* DECIMAL user_type, create_params, auto_increment */
  438.         values           (26,   'precision,scale' ,0, 'decimal')
  439.  
  440.     insert into spt_datatype_info_ext
  441.         /* NUMERIC user_type, create_params, auto_increment */
  442.         values           (25,   'precision,scale' ,0, 'numeric')
  443.  
  444.     insert into spt_datatype_info_ext
  445.         /* DECIMAL IDENTITY user_type, create_params, auto_increment */
  446.         values           (26,   'precision' ,1, 'decimal')
  447.  
  448.     insert into spt_datatype_info_ext
  449.         /* NUMERIC IDENTITY user_type, create_params, auto_increment */
  450.         values           (25,   'precision' ,1, 'numeric')
  451.  
  452. end
  453. else    /*  Pre 6.0 server, add SYSNAME create param */
  454.     begin
  455.         insert into spt_datatype_info_ext
  456.             /* SYSNAME   user_type, create_param, auto_increments */
  457.             values           (18,   'max length' ,0, 'sysname')
  458.  
  459.     end
  460. go
  461.  
  462. if (charindex('7.00', @@version) = 0 and
  463.     charindex('8.00', @@version) = 0)
  464. begin
  465.     print ''
  466.     print ''
  467.     print 'Warning:'
  468.     print 'you are installing the stored procedures '
  469.     print 'on a pre 8.0 SQL Server.'
  470.     print 'Ignore the following errors.'
  471. end
  472. go
  473. if (charindex('7.00', @@version) > 0 or
  474.     charindex('8.00', @@version) > 0)
  475. begin   /*  Update usertypes for 8.0 server */
  476.     begin tran
  477.     insert into spt_datatype_info_ext
  478.         /* NCHAR     user_type, create_params, auto_increment */
  479.         values           (0,    'length' ,0, 'nchar')
  480.  
  481.     insert into spt_datatype_info_ext
  482.         /* NVARCHAR  user_type, create_params, auto_increment */
  483.         values           (0,    'max length' ,0, 'nvarchar')
  484.  
  485.     /* SET user_type TO SPHINX VALUES */
  486.     update spt_datatype_info_ext set user_type = xusertype
  487.         from spt_datatype_info_ext e, systypes t where t.name = e.typename
  488.  
  489.     commit tran
  490. end
  491. go
  492.  
  493. create unique clustered index datatypeinfoextclust on spt_datatype_info_ext(user_type,AUTO_INCREMENT)
  494. go
  495.  
  496. print 'creating table spt_datatype_info'
  497. go
  498. if (charindex('6.00', @@version) = 0 and
  499.     charindex('6.50', @@version) = 0 and
  500.     charindex('7.00', @@version) = 0 and
  501.     charindex('8.00', @@version) = 0)
  502. begin   /*  Pre 6.0 server */
  503.     print ''
  504.     print ''
  505.     print 'Warning:'
  506.     print 'you are installing the stored procedures '
  507.     print 'on a pre 6.0 SQL Server.'
  508.     print 'Ignore the following error.'
  509.     create table spt_datatype_info (
  510.         ss_dtype            tinyint     not null,
  511.         fixlen              int         null,       /* datatype len for variable, else null */
  512.         ODBCVer             tinyint     null,       /* version if needed, else null */
  513.         TYPE_NAME           varchar(32) not null,
  514.         DATA_TYPE           smallint    not null,
  515.         data_precision      int         null,
  516.         numeric_scale       smallint    null,   /* min scale if 6.0 */
  517.         RADIX               smallint    null,
  518.         length              int         null,
  519.         LITERAL_PREFIX      varchar(32) null,
  520.         LITERAL_SUFFIX      varchar(32) null,
  521.         CREATE_PARAMS       varchar(32) null,
  522.         NULLABLE            smallint    not null,
  523.         CASE_SENSITIVE      smallint    not null,
  524.         SEARCHABLE          smallint    not null,
  525.         UNSIGNED_ATTRIBUTE  smallint    null,
  526.         MONEY               smallint    not null,
  527.         AUTO_INCREMENT      smallint    null,
  528.         LOCAL_TYPE_NAME     varchar(32) null,
  529.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  530.         SQL_DATA_TYPE       smallint    not null,
  531.         SQL_DATETIME_SUB    smallint    null)
  532. end
  533. go
  534. if (charindex('6.00', @@version) > 0 or
  535.     charindex('6.50', @@version) > 0 or
  536.     charindex('7.00', @@version) > 0 or
  537.     charindex('8.00', @@version) > 0)
  538. begin   /* 6.0 or later server */
  539.     create table spt_datatype_info (
  540.         ss_dtype            tinyint     not null,
  541.         fixlen              int         null,       /* datatype len for variable, else null */
  542.         ODBCVer             tinyint     null,       /* version if needed, else null */
  543.         TYPE_NAME           sysname     not null,
  544.         DATA_TYPE           smallint    not null,
  545.         data_precision      int         null,
  546.         numeric_scale       smallint    null,   /* min scale if 6.0 */
  547.         RADIX               smallint    null,
  548.         length              int         null,
  549.         LITERAL_PREFIX      varchar(32) null,
  550.         LITERAL_SUFFIX      varchar(32) null,
  551.         CREATE_PARAMS       varchar(32) null,
  552.         NULLABLE            smallint    not null,
  553.         CASE_SENSITIVE      smallint    not null,
  554.         SEARCHABLE          smallint    not null,
  555.         UNSIGNED_ATTRIBUTE  smallint    null,
  556.         MONEY               smallint    not null,
  557.         AUTO_INCREMENT      smallint    null,
  558.         LOCAL_TYPE_NAME     sysname     null,
  559.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  560.         SQL_DATA_TYPE       smallint    not null,
  561.         SQL_DATETIME_SUB    smallint    null)
  562. end
  563. go
  564.  
  565. grant select on spt_datatype_info to public
  566. go
  567.  
  568. /* Get case sensitivity */
  569. if 'A' = 'A' /* create dummy begin block */
  570. begin
  571.     declare @case smallint
  572.  
  573.     begin tran
  574.     select @case = 0
  575.     select @case = 1 where 'a' <> 'A'
  576.  
  577.     /* Local Binary */
  578.     insert into spt_datatype_info values
  579.     (45,null,null,'binary',-2,null,null,null,null,'0x',null,'length',1,0,2,null,0,null,'binary',0,-2,null)
  580.  
  581.     /* Local Bit */
  582.     insert into spt_datatype_info values
  583.     (50,null,null,'bit',-7,1,0,null,1,null,null,null,0,0,2,null,0,null,'bit',null,-7,null)
  584.  
  585.     /* Local Char */
  586.     insert into spt_datatype_info values
  587.     (47,null,null,'char',1,null,null,null,null,'''','''','length',1,@case,3,null,0,null,'char',0,1,null)
  588.  
  589.     /* Local Datetime */
  590.     insert into spt_datatype_info values
  591.     (61,8,2,'datetime',11,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  592.     insert into spt_datatype_info values
  593.     (61,8,3,'datetime',93,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  594.  
  595.     /* Local Smalldatetime */
  596.     insert into spt_datatype_info values
  597.     (58,4,2,'smalldatetime',11,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  598.     insert into spt_datatype_info values
  599.     (58,4,3,'smalldatetime',93,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  600.  
  601.     /* Local Float */
  602.     insert into spt_datatype_info values
  603.     (62,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  604.     insert into spt_datatype_info values
  605.     (62,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  606.  
  607.     /* Local Real */
  608.     insert into spt_datatype_info values
  609.     (59,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  610.     insert into spt_datatype_info values
  611.     (59,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  612.  
  613.     /* Local Smallmoney */
  614.     insert into spt_datatype_info values
  615.     (122,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  616.  
  617.     /* Local Money */
  618.     insert into spt_datatype_info values
  619.     (60,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  620.  
  621.     /* Local Int */
  622.     insert into spt_datatype_info values
  623.     (56,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  624.  
  625.     commit tran
  626. end
  627. go
  628.  
  629. if 'A' = 'A' /* create dummy begin block */
  630. begin
  631.     declare @case smallint
  632.  
  633.     begin tran
  634.     select @case = 0
  635.     select @case = 1 where 'a' <> 'A'
  636.  
  637.  
  638.     /* Local Smallint */
  639.     insert into spt_datatype_info values
  640.     (52,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  641.     insert into spt_datatype_info values
  642.     (52,2,1,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  643.  
  644.     /* Local Tinyint */
  645.     insert into spt_datatype_info values
  646.     (48,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  647.  
  648.     /* Local Text */
  649.     insert into spt_datatype_info values
  650.     (35,null,null,'text',-1,2147483647,null,null,2147483647,'''','''',null,1,@case,1,null,0,null,'text',0,-1,null)
  651.  
  652.     /* Local Varbinary */
  653.     insert into spt_datatype_info values
  654.     (37,null,null,'varbinary',-3,null,null,null,null,'0x',null,'max length',1,0,2,null,0,null,'varbinary',0,-3,null)
  655.  
  656.     /* Local Varchar */
  657.     insert into spt_datatype_info values
  658.     (39,null,null,'varchar',12,null,null,null,null,'''','''','max length',1,@case,3,null,0,null,'varchar',0,12,null)
  659.  
  660.     /* Local Image */
  661.     insert into spt_datatype_info values
  662.     (34,null,null,'image',-4,2147483647,null,null,2147483647,'0x',null,null,1,0,0,null,0,null,'image',0,-4,null)
  663.  
  664.     if (charindex('6.00', @@version) > 0 or
  665.         charindex('6.50', @@version) > 0 or
  666.         charindex('7.00', @@version) > 0 or
  667.         charindex('8.00', @@version) > 0)
  668.     begin   /*  Add 6.0 data types */
  669.         /* Local Decimal */
  670.         insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  671.         (55,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  672.  
  673.         /* Local Numeric */
  674.         insert into spt_datatype_info values    /* sql server type is 'numericn' */
  675.         (63,null,null,'numeric',2   ,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  676.  
  677.         /* Identity attribute data types */
  678.  
  679.         /* Identity Int */
  680.         insert into spt_datatype_info values
  681.         (56,null,null,'int identity',4,10,0,10,null,null,null,null,0,0,2,0,0,1,'int identity',null,4,null)
  682.  
  683.         /* Identity Smallint */
  684.         insert into spt_datatype_info values
  685.         (52,null,null,'smallint identity',5,5,0,10,null,null,null,null,0,0,2,0,0,1,'smallint identity',null,5,null)
  686.  
  687.         /* Identity Tinyint */
  688.         insert into spt_datatype_info values
  689.         (48,null,null,'tinyint identity',-6,3,0,10,null,null,null,null,0,0,2,1,0,1,'tinyint identity',null,-6,null)
  690.  
  691.         /* Identity Numeric */
  692.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  693.         (55,null,null,'decimal() identity',3,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'decimal() identity',null,3,null)
  694.  
  695.         /* Identity Numeric */
  696.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  697.         (63,null,null,'numeric() identity',2,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'numeric() identity',null,2,null)
  698.  
  699.     end
  700.  
  701.     if (charindex('7.00', @@version) = 0 and
  702.         charindex('8.00', @@version) = 0)
  703.     begin   /*  Add nullable type for non-8.0 server */
  704.         /* Local Datetimn */
  705.         insert into spt_datatype_info values
  706.         (111,4,2,'smalldatetime',11,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  707.         insert into spt_datatype_info values
  708.         (111,4,3,'smalldatetime',93,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  709.         insert into spt_datatype_info values /* sql server type is 'datetimn' */
  710.         (111,8,2,'datetime',11,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  711.         insert into spt_datatype_info values
  712.         (111,8,3,'datetime',93,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  713.  
  714.         /* Local Floatn */
  715.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  716.         (109,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  717.         insert into spt_datatype_info values
  718.         (109,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  719.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  720.         (109,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  721.         insert into spt_datatype_info values
  722.         (109,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  723.  
  724.         /* Local Moneyn */
  725.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  726.         (110,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  727.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  728.         (110,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  729.  
  730.         /* Local Intn */
  731.         insert into spt_datatype_info values /* sql server type is 'intn' */
  732.         (38,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  733.         insert into spt_datatype_info values /* sql server type is 'intn' */
  734.         (38,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  735.         insert into spt_datatype_info values
  736.         (38,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  737.  
  738.         if (charindex('6.00', @@version) > 0 or
  739.             charindex('6.50', @@version) > 0 or
  740.             charindex('7.00', @@version) > 0 or
  741.             charindex('8.00', @@version) > 0)
  742.         begin   /*  Add 6.0 data types */
  743.             /* Local Decimaln */
  744.             insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  745.             (106,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  746.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  747.             (106,null,null,'decimal() identity',3,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'decimal() identity',null,3,null)
  748.  
  749.             /* Local Numericn */
  750.             insert into spt_datatype_info values    /* sql server type is 'numericn' */
  751.             (108,null,null,'numeric',2,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  752.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  753.             (108,null,null,'numeric() identity',2,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'numeric() identity',null,2,null)
  754.         end
  755.     end
  756.  
  757.     commit tran
  758. end
  759. go
  760.  
  761. if (charindex('7.00', @@version) = 0 and
  762.     charindex('8.00', @@version) = 0)
  763. begin
  764.     print ''
  765.     print ''
  766.     print 'Warning:'
  767.     print 'you are installing the stored procedures '
  768.     print 'on a pre 8.0 SQL Server.'
  769.     print 'Ignore the following errors.'
  770. end
  771. go
  772. if (charindex('7.00', @@version) > 0 or
  773.     charindex('8.00', @@version) > 0)
  774.     begin
  775.         declare @ncase smallint
  776.  
  777.         select @ncase = 0
  778.         select @ncase = 1 where N'a' <> N'A'
  779.  
  780.         /* Local Timestamp */
  781.         insert into spt_datatype_info values
  782.         (0,null,null,'timestamp',-2,8,null,null,null,'0x',null,null,1,0,2,null,0,null,'timestamp',0,-2,null)
  783.  
  784.         /* Local GUID */
  785.         insert into spt_datatype_info values
  786.         (0,null,null,'uniqueidentifier',-11,36,null,null,null,'''','''',null,1,0,2,null,0,null,'uniqueidentifier',NULL,-11,null)
  787.  
  788.         /* Local NChar */
  789.         insert into spt_datatype_info values
  790.         (0,null,null,'nchar',-8,null,null,null,null,'N''','''','length',1,@ncase,3,null,0,null,'nchar',0,-8,null)
  791.  
  792.         /* Local NVarchar */
  793.         insert into spt_datatype_info values
  794.         (0,null,null,'nvarchar',-9,null,null,null,null,'N''','''','max length',1,@ncase,3,null,0,null,'nvarchar',0,-9,null)
  795.  
  796.         /* Local NText */
  797.         insert into spt_datatype_info values
  798.         (0,null,null,'ntext',-10,2147483646,null,null,2147483646,'N''','''',null,1,@ncase,1,null,0,null,'ntext',0,-10,null)
  799.  
  800.         if (charindex('8.00', @@version) > 0)
  801.         begin
  802. --          /* Local BIGINT */
  803.             insert into spt_datatype_info values
  804.             (127,8,null,'bigint',-5,19,0,10,null,null,null,null,1,0,2,0,0,0,'bigint',null,-5,null)
  805. --          /* Identity BIGINT */
  806.             insert into spt_datatype_info values
  807.             (127,8,null,'bigint identity',-5,19,0,10,null,null,null,null,0,0,2,0,0,1,'bigint identity',null,-5,null)
  808.  
  809.  
  810. --          /* sql_variant */
  811.             insert into spt_datatype_info values
  812.             (
  813.             98,                 --ss_dtype      
  814.             null,               --fixlen
  815.             null,               --ODBCVer
  816.             'sql_variant',      --TYPE_NAME
  817.             -150,               --SQL DATA TYPE
  818.             8000,               --data_precision
  819.             0,                  --numeric_scale
  820.             10,                 --RADIX
  821.             8000,               --length
  822.             null,               --PREFIX
  823.             null,               --SUFFIX
  824.             null,               --Create Params
  825.             1,                  --Nullable
  826.             0,              --Case sensitive
  827.             2,                  --Searchable
  828.             null,               --UNSIGNED_ATTRIBUTE
  829.             0,              --MONEY
  830.             null,               --AUTO_INCREMENT
  831.             'sql_variant',      --LOCAL TYPE NAME
  832.             0,                  --CHARBIN
  833.             -150,               --SQL_DATA_TYPE
  834.             null                --SQL_DATETIME_SUB
  835.             )
  836.         
  837.         end
  838.  
  839.  
  840.  
  841.         update spt_datatype_info set NULLABLE = 1
  842.             where TYPE_NAME = 'bit'
  843.  
  844.         update spt_datatype_info set ss_dtype = isnull((select distinct xtype from systypes
  845.             where TYPE_NAME like name+'%'),0)
  846.     end
  847. go
  848.  
  849. create unique clustered index datatypeinfoclust on spt_datatype_info(ss_dtype,fixlen,ODBCVer,AUTO_INCREMENT)
  850. go
  851.  
  852. dump tran master with no_log
  853. go
  854.  
  855. print 'creating table spt_server_info'
  856. go
  857. create table spt_server_info (
  858.               attribute_id      int NOT NULL,
  859.               attribute_name    varchar(60) NOT NULL,
  860.               attribute_value   varchar(255) NOT NULL)
  861. go
  862.  
  863. create unique clustered index serverinfoclust on spt_server_info(attribute_id)
  864. go
  865.  
  866. if (charindex('7.00', @@version) = 0 and charindex('8.00', @@version) = 0)
  867.     begin
  868.         drop procedure sp_add_server_sortinfo   /* not used by other servers */
  869.         drop procedure sp_add_server_sortinfo75 /* not used by older servers */
  870.         dump tran master with no_log
  871.     end
  872. go
  873.  
  874.  
  875. insert into spt_server_info
  876.     values (1,'DBMS_NAME','Microsoft SQL Server')
  877. insert into spt_server_info
  878.     values (2,'DBMS_VER',@@version)
  879. insert into spt_server_info
  880.     values (10,'OWNER_TERM','owner')
  881. insert into spt_server_info
  882.     values (11,'TABLE_TERM','table')
  883. insert into spt_server_info
  884.     values (12,'MAX_OWNER_NAME_LENGTH','30')
  885. insert into spt_server_info
  886.     values (13,'TABLE_LENGTH','30')
  887. insert into spt_server_info
  888.     values (14,'MAX_QUAL_LENGTH','30')
  889. insert into spt_server_info
  890.     values (15,'COLUMN_LENGTH','30')
  891. if 'A' = 'a' /* If not case sensitive server */
  892. begin
  893.     insert into spt_server_info
  894.         values (16,'IDENTIFIER_CASE','MIXED')
  895. end
  896. else
  897. begin
  898.     insert into spt_server_info
  899.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  900. end
  901. insert into spt_server_info
  902.     values (17,'TX_ISOLATION','2')
  903. if (charindex('6.00', @@version) > 0 or
  904.     charindex('6.50', @@version) > 0 or
  905.     charindex('7.00', @@version) > 0) 
  906. begin   /*  Add 6.0 collation sequence */
  907.     insert into spt_server_info
  908.         select 18,'COLLATION_SEQ',
  909.             'charset='+t2.name+' sort_order='+t1.name
  910.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  911.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  912.         from syscharsets t1, syscharsets t2, sysconfigures t3
  913.         where t1.csid=t2.id and t1.id=t3.value and t3.config=1123
  914. end
  915. else 
  916. begin   /*  Add 4.2x collation sequence */
  917.     insert into spt_server_info
  918.         select 18,'COLLATION_SEQ',
  919.             'charset='+t2.name+' sort_order='+t1.name
  920.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  921.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  922.         from syscharsets t1, syscharsets t2, sysconfigures t3
  923.         where t1.csid=t2.id and t1.id=t3.value and t3.config=123
  924. end
  925. insert into spt_server_info
  926.     values (19,'SAVEPOINT_SUPPORT','Y')
  927. insert into spt_server_info
  928.     values (20,'MULTI_RESULT_SETS','Y')
  929. insert into spt_server_info
  930.     values (22,'ACCESSIBLE_TABLES','Y')
  931. go
  932.  
  933. insert into spt_server_info
  934.     values (100,'USERID_LENGTH','30')
  935. insert into spt_server_info
  936.     values (101,'QUALIFIER_TERM','database')
  937. insert into spt_server_info
  938.     values (102,'NAMED_TRANSACTIONS','Y')
  939. insert into spt_server_info
  940.     values (103,'SPROC_AS_LANGUAGE','Y')
  941. insert into spt_server_info
  942.     values (104,'ACCESSIBLE_SPROC','Y')
  943. insert into spt_server_info
  944.     values (105,'MAX_INDEX_COLS','16')
  945. insert into spt_server_info
  946.     values (106,'RENAME_TABLE','Y')
  947. insert into spt_server_info
  948.     values (107,'RENAME_COLUMN','Y')
  949.  
  950. if (charindex('8.00', @@version) > 0)
  951. begin
  952.     /* Columns may be dropped on 8.0 or later */
  953.     insert into spt_server_info
  954.         values (108,'DROP_COLUMN','Y')
  955. end
  956. else
  957. begin
  958.     insert into spt_server_info
  959.         values (108,'DROP_COLUMN','N')
  960. end
  961.  
  962. if (charindex('8.00', @@version) > 0)
  963. begin
  964.     /* Columns size may be changed on 8.0 or later */
  965.     insert into spt_server_info
  966.         values (109,'INCREASE_COLUMN_LENGTH','Y')
  967. end
  968. else
  969. begin
  970.     insert into spt_server_info
  971.         values (109,'INCREASE_COLUMN_LENGTH','N')
  972. end
  973.  
  974. if (charindex('6.50', @@version) = 0 and
  975.     charindex('7.00', @@version) = 0 and
  976.     charindex('8.00', @@version) = 0)
  977. begin
  978.     insert into spt_server_info
  979.         values (110,'DDL_IN_TRANSACTION','N')
  980. end
  981. else
  982. begin
  983.     insert into spt_server_info
  984.         values (110,'DDL_IN_TRANSACTION','Y')
  985. end
  986.  
  987.  
  988. if (charindex('8.00', @@version) > 0)
  989. begin
  990.     /* Descending indexes allowed on 8.0 or later */
  991.     insert into spt_server_info
  992.         values (111,'DESCENDING_INDEXES','Y')
  993. end
  994. else
  995. begin
  996.     insert into spt_server_info
  997.         values (111,'DESCENDING_INDEXES','N')
  998. end
  999.  
  1000. insert into spt_server_info
  1001.     values (112,'SP_RENAME','Y')
  1002. insert into spt_server_info
  1003.     values (113,'REMOTE_SPROC','Y')
  1004. insert into spt_server_info
  1005.     values (500,'SYS_SPROC_VERSION','8.00.552')
  1006. go
  1007.  
  1008. if (charindex('7.00', @@version) > 0 or
  1009.     charindex('8.00', @@version) > 0)
  1010. begin   /*  Update values for 8.0 server */
  1011.     update spt_server_info set attribute_value = '128'
  1012.         where attribute_id in (12,13,14,15,100)
  1013. end
  1014. go
  1015.  
  1016. grant select on spt_server_info to public
  1017. go
  1018.  
  1019. print 'creating sp_column_privileges'
  1020. go
  1021.  
  1022. /*  Procedure for pre 6.50 server */
  1023. CREATE PROCEDURE sp_column_privileges (
  1024.             @table_name         varchar(32),
  1025.             @table_owner        varchar(32) = null,
  1026.             @table_qualifier    varchar(32) = null,
  1027.             @column_name        varchar(96) = null) /* 3*32 */
  1028. as
  1029.  
  1030.     set nocount on
  1031.     declare @table_id    int
  1032.     DECLARE @full_table_name    varchar(65) /* 2*32 + 1 */
  1033.     declare @low smallint                    /* range of userids to check */
  1034.     declare @high smallint
  1035.     declare @owner_uid smallint
  1036.  
  1037.     select @low = 0, @high = 32767
  1038.  
  1039.     if @column_name is null /*  If column name not supplied, match all */
  1040.         select @column_name = '%'
  1041.  
  1042.     if @table_qualifier is not null
  1043.     begin
  1044.         if db_name() <> @table_qualifier
  1045.         begin   /* If qualifier doesn't match current database */
  1046.             raiserror 20001 '~~Rush_5~~'
  1047.             return
  1048.         end
  1049.     end
  1050.     if @table_owner is null
  1051.     begin   /* If unqualified table name */
  1052.         SELECT @full_table_name = @table_name
  1053.     end
  1054.     else
  1055.     begin   /* Qualified table name */
  1056.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1057.     end
  1058.     /*  Get Object ID */
  1059.     select @table_id = object_id(@full_table_name)
  1060.  
  1061.     if (@@trancount <> 0)
  1062.     begin   /* If inside a transaction */
  1063.         raiserror 20003 '~~Rush_6~~'
  1064.         return
  1065.     end
  1066.  
  1067.     /*
  1068.     ** We need to create a table which will contain a row for every row to
  1069.     ** be returned to the client.
  1070.     */
  1071.  
  1072.     create table #column_priv1(
  1073.         COLUMN_NAME             varchar(32) NOT NULL,
  1074.         grantor                 smallint NOT NULL,
  1075.         grantee                 smallint NOT NULL,
  1076.         select_privilege        bit,
  1077.         select_grantable        bit,
  1078.         insert_privilege        bit,
  1079.         insert_grantable        bit,
  1080.         update_privilege        bit,
  1081.         update_grantable        bit,
  1082.         references_privilege    bit,
  1083.         references_grantable    bit,
  1084.         uid                     smallint NOT NULL,
  1085.         gid                     smallint NOT NULL)
  1086.  
  1087. /*
  1088. ** insert a row for the table owner (who has all permissions)
  1089. */
  1090.     select @owner_uid = (
  1091.         select uid
  1092.         from sysobjects
  1093.         where id = @table_id)
  1094.  
  1095.     if (charindex('6.00', @@version) > 0)
  1096.     begin
  1097.         insert into #column_priv1
  1098.             select
  1099.                 c.name,
  1100.                 u.uid,
  1101.                 @owner_uid,
  1102.                 0,
  1103.                 1,
  1104.                 0,
  1105.                 1,
  1106.                 0,
  1107.                 1,
  1108.                 0,
  1109.                 1,
  1110.                 @owner_uid,
  1111.                 0
  1112.             from syscolumns c, sysusers u
  1113.             where id = @table_id
  1114.                 and c.number = 0
  1115.                 and u.uid = 1       /* grantor is dbo of database */
  1116.     end
  1117.     else
  1118.     begin
  1119.         insert into #column_priv1
  1120.             select
  1121.                 c.name,
  1122.                 u.uid,
  1123.                 @owner_uid,
  1124.                 0,
  1125.                 1,
  1126.                 0,
  1127.                 1,
  1128.                 0,
  1129.                 1,
  1130.                 0,
  1131.                 0,
  1132.                 @owner_uid,
  1133.                 0
  1134.             from syscolumns c, sysusers u
  1135.             where id = @table_id
  1136.                 and c.number = 0
  1137.                 and u.uid = 1       /* grantor is dbo of database */
  1138.     end
  1139. /*
  1140. ** now stick in a row for every column for every user in the database
  1141. ** we will need to weed out those who have no permissions later
  1142. ** (and yes this is a cartesion product: the uid field in sysprotects
  1143. ** can also have a group id, in which case we need to extend those
  1144. ** privileges to all group members).
  1145. */
  1146.  
  1147.     insert into #column_priv1
  1148.         select distinct
  1149.             c.name,
  1150.             o.uid,
  1151.             u.uid,
  1152.             0,
  1153.             0,
  1154.             0,
  1155.             0,
  1156.             0,
  1157.             0,
  1158.             0,
  1159.             0,
  1160.             u.uid,
  1161.             u.gid
  1162.         from sysusers u, syscolumns c, sysobjects o
  1163.         where o.id = @table_id
  1164.             and c.id = o.id
  1165.             and c.number = 0
  1166.             and u.gid <> u.uid
  1167.             and u.uid <> @owner_uid
  1168.  
  1169.     /*
  1170.     ** we need to create another temporary table to contain all the various
  1171.     ** protection information for the table in question
  1172.     */
  1173.     create table #protects (
  1174.                 uid         smallint NOT NULL,
  1175.                 grantor     smallint NOT NULL,
  1176.                 action      tinyint NOT NULL,
  1177.                 protecttype tinyint NOT NULL,
  1178.                 name        varchar(32) NOT NULL)
  1179.  
  1180.     insert into #protects
  1181.         select
  1182.             p.uid,
  1183.             p.uid,
  1184.             p.action,
  1185.             p.protecttype,
  1186.             isnull(col_name(id, c.number), '~All')
  1187.             from
  1188.                 sysprotects p,
  1189.                 master.dbo.spt_values c,
  1190.                 master.dbo.spt_values a,
  1191.                 master.dbo.spt_values b
  1192.             where
  1193.                 convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  1194.                     & c.high <> 0
  1195.                     and c.number <= (
  1196.                         select count(*)
  1197.                         from syscolumns
  1198.                         where id = @table_id)
  1199.                 and c.type = 'P'
  1200.                 and a.type = 'T'
  1201.                 and a.number = p.action
  1202.                 and p.action in (193,195,197,26)
  1203.                 and b.type = 'T'
  1204.                 and b.number = p.protecttype
  1205.                 and p.id = @table_id
  1206.                 and p.uid between @low and @high
  1207.  
  1208.  
  1209.     update #column_priv1
  1210.     set select_privilege = 1
  1211.         from #protects p
  1212.     where
  1213.         p.protecttype = 205
  1214.         and p.action = 193
  1215.         and (p.name = #column_priv1.COLUMN_NAME
  1216.             or name = '~All')
  1217.         and (p.uid = 0
  1218.             or p.uid = #column_priv1.gid
  1219.             or p.uid = #column_priv1.uid)
  1220.         and not exists (
  1221.             select * from #protects
  1222.             where
  1223.                 protecttype = 206
  1224.                 and action = 193
  1225.                 and (name = #column_priv1.COLUMN_NAME
  1226.                     or name = '~All')
  1227.                 and ( uid = 0
  1228.                     or uid = #column_priv1.gid
  1229.                     or uid = #column_priv1.uid))
  1230.  
  1231.     update #column_priv1
  1232.     set insert_privilege = 1
  1233.         from #protects p
  1234.     where
  1235.         p.protecttype = 205
  1236.         and p.action = 195
  1237.         and (p.name = #column_priv1.COLUMN_NAME
  1238.             or name = '~All')
  1239.         and (p.uid = 0
  1240.             or p.uid = #column_priv1.gid
  1241.             or p.uid = #column_priv1.uid)
  1242.         and not exists (
  1243.             select * from #protects
  1244.             where
  1245.                 protecttype = 206
  1246.                 and action = 195
  1247.                 and (name = #column_priv1.COLUMN_NAME
  1248.                     or name = '~All')
  1249.                 and (uid = 0
  1250.                     or uid = #column_priv1.gid
  1251.                     or uid = #column_priv1.uid))
  1252.  
  1253.     update #column_priv1
  1254.     set update_privilege = 1
  1255.         from #protects p
  1256.     where
  1257.         p.protecttype = 205
  1258.         and p.action = 197
  1259.         and (p.name = #column_priv1.COLUMN_NAME
  1260.             or name = '~All')
  1261.         and (p.uid = 0
  1262.             or p.uid = #column_priv1.gid
  1263.             or p.uid = #column_priv1.uid)
  1264.         and not exists (
  1265.             select * from #protects
  1266.                 where protecttype = 206
  1267.                 and action = 197
  1268.                 and (name = #column_priv1.COLUMN_NAME
  1269.                     or name = '~All')
  1270.                 and (uid = 0
  1271.                     or uid = #column_priv1.gid
  1272.                     or uid = #column_priv1.uid))
  1273.  
  1274.     update #column_priv1
  1275.     set references_privilege = 1
  1276.         from #protects p
  1277.     where
  1278.         p.protecttype = 205
  1279.         and p.action = 26
  1280.         and (p.name = #column_priv1.COLUMN_NAME
  1281.             or name = '~All')
  1282.         and (p.uid = 0
  1283.             or p.uid = #column_priv1.gid
  1284.             or p.uid = #column_priv1.uid)
  1285.         and not exists (
  1286.             select * from #protects
  1287.                 where protecttype = 206
  1288.                 and action = 26
  1289.                 and (name = #column_priv1.COLUMN_NAME
  1290.                     or name = '~All')
  1291.                 and (uid = 0
  1292.                     or uid = #column_priv1.gid
  1293.                     or uid = #column_priv1.uid))
  1294.  
  1295.     update #column_priv1
  1296.     set select_grantable = 1
  1297.         from #protects p
  1298.     where
  1299.         p.protecttype = 204
  1300.         and p.action = 193
  1301.         and (p.name = #column_priv1.COLUMN_NAME
  1302.             or name = '~All')
  1303.         and (p.uid = 0
  1304.             or p.uid = #column_priv1.gid
  1305.             or p.uid = #column_priv1.uid)
  1306.         and not exists (
  1307.             select * from #protects
  1308.             where
  1309.                 protecttype = 206
  1310.                 and action = 193
  1311.                 and (name = #column_priv1.COLUMN_NAME
  1312.                     or name = '~All')
  1313.                 and ( uid = 0
  1314.                     or uid = #column_priv1.gid
  1315.                     or uid = #column_priv1.uid))
  1316.  
  1317.     update #column_priv1
  1318.     set insert_grantable = 1
  1319.         from #protects p
  1320.     where
  1321.         p.protecttype = 204
  1322.         and p.action = 195
  1323.         and (p.name = #column_priv1.COLUMN_NAME
  1324.             or name = '~All')
  1325.         and (p.uid = 0
  1326.             or p.uid = #column_priv1.gid
  1327.             or p.uid = #column_priv1.uid)
  1328.         and not exists (
  1329.             select * from #protects
  1330.             where
  1331.                 protecttype = 206
  1332.                 and action = 195
  1333.                 and (name = #column_priv1.COLUMN_NAME
  1334.                     or name = '~All')
  1335.                 and ( uid = 0
  1336.                     or uid = #column_priv1.gid
  1337.                     or uid = #column_priv1.uid))
  1338.  
  1339.     update #column_priv1
  1340.     set update_grantable = 1
  1341.         from #protects p
  1342.     where
  1343.         p.protecttype = 204
  1344.         and p.action = 197
  1345.         and (p.name = #column_priv1.COLUMN_NAME
  1346.             or name = '~All')
  1347.         and (p.uid = 0
  1348.             or p.uid = #column_priv1.gid
  1349.             or p.uid = #column_priv1.uid)
  1350.         and not exists (
  1351.             select * from #protects
  1352.             where
  1353.                 protecttype = 206
  1354.                 and action = 197
  1355.                 and (name = #column_priv1.COLUMN_NAME
  1356.                     or name = '~All')
  1357.                 and ( uid = 0
  1358.                     or uid = #column_priv1.gid
  1359.                     or uid = #column_priv1.uid))
  1360.  
  1361.     update #column_priv1
  1362.     set references_grantable = 1
  1363.         from #protects p
  1364.     where
  1365.         p.protecttype = 204
  1366.         and p.action = 26
  1367.         and (p.name = #column_priv1.COLUMN_NAME
  1368.             or name = '~All')
  1369.         and (p.uid = 0
  1370.             or p.uid = #column_priv1.gid
  1371.             or p.uid = #column_priv1.uid)
  1372.         and not exists (
  1373.             select * from #protects
  1374.             where
  1375.                 protecttype = 206
  1376.                 and action = 26
  1377.                 and (name = #column_priv1.COLUMN_NAME
  1378.                     or name = '~All')
  1379.                 and ( uid = 0
  1380.                     or uid = #column_priv1.gid
  1381.                     or uid = #column_priv1.uid))
  1382.  
  1383.     create table #column_priv2(
  1384.         COLUMN_NAME     varchar(32) NOT NULL,
  1385.         grantor         smallint NULL,
  1386.         grantee         smallint NOT NULL,
  1387.         PRIVILEGE       varchar(32) NOT NULL,
  1388.         IS_GRANTABLE    varchar(3) NULL)
  1389.  
  1390.     insert into #column_priv2
  1391.         select
  1392.             COLUMN_NAME,
  1393.             grantor,
  1394.             grantee,
  1395.             'SELECT',
  1396.             'NO'
  1397.         from #column_priv1
  1398.         where select_privilege = 1 and select_grantable = 0
  1399.  
  1400.     insert into #column_priv2
  1401.         select
  1402.             COLUMN_NAME,
  1403.             grantor,
  1404.             grantee,
  1405.             'INSERT',
  1406.             'NO'
  1407.         from #column_priv1
  1408.         where insert_privilege = 1 and insert_grantable = 0
  1409.  
  1410.     insert into #column_priv2
  1411.         select
  1412.             COLUMN_NAME,
  1413.             grantor,
  1414.             grantee,
  1415.             'UPDATE',
  1416.             'NO'
  1417.         from #column_priv1
  1418.         where update_privilege = 1 and update_grantable = 0
  1419.  
  1420.     insert into #column_priv2
  1421.         select
  1422.             COLUMN_NAME,
  1423.             grantor,
  1424.             grantee,
  1425.             'REFERENCES',
  1426.             'NO'
  1427.         from #column_priv1
  1428.         where references_privilege = 1 and references_grantable = 0
  1429.  
  1430.     insert into #column_priv2
  1431.         select
  1432.             COLUMN_NAME,
  1433.             grantor,
  1434.             grantee,
  1435.             'SELECT',
  1436.             'YES'
  1437.         from #column_priv1
  1438.         where select_grantable = 1
  1439.  
  1440.     insert into #column_priv2
  1441.         select
  1442.             COLUMN_NAME,
  1443.             grantor,
  1444.             grantee,
  1445.             'INSERT',
  1446.             'YES'
  1447.         from #column_priv1
  1448.         where insert_grantable = 1
  1449.  
  1450.     insert into #column_priv2
  1451.         select
  1452.             COLUMN_NAME,
  1453.             grantor,
  1454.             grantee,
  1455.             'UPDATE',
  1456.             'YES'
  1457.         from #column_priv1
  1458.         where update_grantable = 1
  1459.  
  1460.     insert into #column_priv2
  1461.         select
  1462.             COLUMN_NAME,
  1463.             grantor,
  1464.             grantee,
  1465.             'REFERENCES',
  1466.             'YES'
  1467.         from #column_priv1
  1468.         where references_grantable = 1
  1469.  
  1470.     select
  1471.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1472.         convert(varchar(32),user_name(@owner_uid)) TABLE_OWNER,
  1473.         @table_name TABLE_NAME,
  1474.         COLUMN_NAME,
  1475.         convert(varchar(32),user_name(grantor)) GRANTOR,
  1476.         convert(varchar(32),user_name(grantee)) GRANTEE,
  1477.         PRIVILEGE,
  1478.         IS_GRANTABLE
  1479.     from #column_priv2
  1480.     where COLUMN_NAME like @column_name
  1481.     order by 4, 7
  1482. go
  1483.  
  1484. if (charindex('6.50', @@version) = 0 and
  1485.     charindex('7.00', @@version) = 0 and
  1486.     charindex('8.00', @@version) = 0)
  1487. begin
  1488.     print ''
  1489.     print ''
  1490.     print 'Warning:'
  1491.     print 'you are installing the stored procedures '
  1492.     print 'on a pre 6.50 SQL Server.'
  1493.     print 'Ignore the following errors.'
  1494. end
  1495. else
  1496.     drop proc sp_column_privileges
  1497. go
  1498.  
  1499. /*  Procedure for 6.50 server */
  1500. CREATE PROCEDURE sp_column_privileges (
  1501.             @table_name         varchar(32),
  1502.             @table_owner        varchar(32) = null,
  1503.             @table_qualifier    varchar(32) = null,
  1504.             @column_name        varchar(96) = null) /* 3*32 */
  1505. as
  1506.  
  1507.     declare @table_id    int
  1508.  
  1509.     if @column_name is null /*  If column name not supplied, match all */
  1510.         select @column_name = '%'
  1511.  
  1512.     if @table_qualifier is not null
  1513.     begin
  1514.         if db_name() <> @table_qualifier
  1515.         begin   /* If qualifier doesn't match current database */
  1516.             raiserror (15250, -1,-1)
  1517.             return
  1518.         end
  1519.     end
  1520.     if @table_owner is null
  1521.     begin   /* If unqualified table name */
  1522.         select @table_id = object_id(@table_name)
  1523.     end
  1524.     else
  1525.     begin   /* Qualified table name */
  1526.         select @table_id = object_id(@table_owner + '.' + @table_name)
  1527.     end
  1528.  
  1529.     select
  1530.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1531.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1532.         @table_name TABLE_NAME,
  1533.         convert(varchar(32),c.name) COLUMN_NAME,
  1534.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  1535.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  1536.         convert (varchar(32),case p.action
  1537.              when 193 then 'SELECT'
  1538.              when 195 then 'INSERT'
  1539.              when 197 then 'UPDATE'
  1540.              else 'REFERENCES'
  1541.         end) PRIVILEGE,
  1542.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1543.             else 'YES'
  1544.         end) IS_GRANTABLE
  1545.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  1546.     where
  1547.         c.id = @table_id
  1548.         and c.name like @column_name
  1549.         and c.id = p.id
  1550.         and c.id = o.id
  1551.         and case substring(p.columns, 1, 1) & 1
  1552.                 when NULL then 255  /* all columns have permission */
  1553.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1554.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1555.             end
  1556.             & v.high <> 0           /* permission applies to this column */
  1557.         and v.number <= (select count(*) from syscolumns
  1558.             where id = @table_id)   /* ranges from 1 to # of columns in table */
  1559.         and v.type = 'P'
  1560.         and v.number = c.colid
  1561.             /* expand groups */
  1562.         and ((p.uid = u.uid and u.uid <> u.gid) or
  1563.              (p.uid = u.gid and u.uid <> u.gid))
  1564.         and p.protecttype <> 206    /* only grant rows */
  1565.         and p.action in (26,193,195,197)
  1566.         and o.uid <> u.uid          /* no rows for owner */
  1567.         and not exists (            /* exclude revoke'd privileges */
  1568.             select *
  1569.             from sysprotects p1
  1570.             where
  1571.                 p1.protecttype = 206
  1572.                 and p1.action = p.action
  1573.                 and p1.id = p.id
  1574.                 and p1.uid = u.uid
  1575.                 and case substring(p1.columns, 1, 1) & 1
  1576.                         when NULL then 255  /* all columns have permission */
  1577.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1578.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1579.                     end
  1580.                     & v.high <> 0)          /* permission applies to this column */
  1581.     union
  1582.     select  /*  Add rows for table owner */
  1583.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1584.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1585.         @table_name TABLE_NAME,
  1586.         convert(varchar(32),col_name(@table_id, c.colid)) COLUMN_NAME,
  1587.         convert(varchar(32),user_name(u.uid)) grantor,
  1588.         convert(varchar(32),user_name(o.uid)) grantee,
  1589.         convert (varchar(32),case v.number
  1590.             when 193 then 'SELECT'
  1591.             when 195 then 'INSERT'
  1592.             when 197 then 'UPDATE'
  1593.             else 'REFERENCES'
  1594.         end) PRIVILEGE,
  1595.         convert(varchar(3),'YES') IS_GRANTABLE
  1596.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1597.     where
  1598.         c.id = @table_id
  1599.         and c.name like @column_name
  1600.         and c.id = o.id
  1601.         and u.uid = 1       /* grantor is dbo of database */
  1602.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1603.         and v.number in (26,193,195,197)
  1604.         and not exists (    /* exclude revoke'd privileges */
  1605.             select *
  1606.             from sysprotects p1
  1607.             where
  1608.                 p1.protecttype = 206
  1609.                 and p1.action = v.number
  1610.                 and p1.id = o.id
  1611.                 and p1.uid = o.uid)
  1612.     order by 4, 7
  1613. go
  1614.  
  1615.  
  1616.  
  1617. if (charindex('7.00', @@version) = 0 and
  1618.     charindex('8.00', @@version) = 0)
  1619. begin
  1620.     print ''
  1621.     print ''
  1622.     print 'Warning:'
  1623.     print 'you are installing the stored procedures '
  1624.     print 'on a pre 8.0 SQL Server.'
  1625.     print 'Ignore the following errors.'
  1626. end
  1627. else
  1628.     drop proc sp_column_privileges
  1629. go
  1630.  
  1631. /*  Procedure for 8.0 server */
  1632. CREATE PROCEDURE sp_column_privileges (
  1633.             @table_name         sysname,
  1634.             @table_owner        sysname = null,
  1635.             @table_qualifier    sysname = null,
  1636.             @column_name        nvarchar(384) = null)   /* 3*128 */
  1637. as
  1638.  
  1639.     declare @table_id    int
  1640.  
  1641.     if @column_name is null /*  If column name not supplied, match all */
  1642.         select @column_name = '%'
  1643.  
  1644.     if @table_qualifier is not null
  1645.     begin
  1646.         if db_name() <> @table_qualifier
  1647.         begin   /* If qualifier doesn't match current database */
  1648.             raiserror (15250, -1,-1)
  1649.             return
  1650.         end
  1651.     end
  1652.     if @table_owner is null
  1653.     begin   /* If unqualified table name */
  1654.         select @table_id = object_id(quotename(@table_name))
  1655.     end
  1656.     else
  1657.     begin   /* Qualified table name */
  1658.         if @table_owner = N''
  1659.         begin   /* If empty owner name */
  1660.             select @table_id = 0
  1661.         end
  1662.         else
  1663.         begin
  1664.             select @table_id = object_id(quotename(@table_owner) +
  1665.                 '.' + quotename(@table_name))
  1666.         end
  1667.     end
  1668.  
  1669.     select
  1670.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1671.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1672.         @table_name TABLE_NAME,
  1673.         convert(sysname,c.name) COLUMN_NAME,
  1674.         convert(sysname,user_name(p.grantor)) GRANTOR,
  1675.         convert(sysname,user_name(u.uid)) GRANTEE,
  1676.         convert (varchar(32),case p.action
  1677.              when 193 then 'SELECT'
  1678.              when 195 then 'INSERT'
  1679.              when 197 then 'UPDATE'
  1680.              else 'REFERENCES'
  1681.         end) PRIVILEGE,
  1682.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1683.             else 'YES'
  1684.         end) IS_GRANTABLE
  1685.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  1686.     where
  1687.         c.id = @table_id
  1688.         and c.name like @column_name
  1689.         and c.id = p.id
  1690.         and c.id = o.id
  1691.         and case substring(p.columns, 1, 1) & 1
  1692.                 when NULL then 255  /* all columns have permission */
  1693.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1694.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1695.             end
  1696.             & v.high <> 0           /* permission applies to this column */
  1697.         and v.number <= (select count(*) from syscolumns
  1698.             where id = @table_id)   /* ranges from 1 to # of columns in table */
  1699.         and v.type = N'P'
  1700.         and v.number = c.colid
  1701.             /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  1702.         and (u.uid > 0 and u.uid < 16384)
  1703.         and ((p.uid = u.uid) or 
  1704.              (p.uid = m.groupuid and u.uid = m.memberuid))
  1705.         and p.protecttype <> 206    /* only grant rows */
  1706.         and p.action in (26,193,195,197)
  1707.         and o.uid <> u.uid          /* no rows for owner */
  1708.         and not exists (            /* exclude revoke'd privileges */
  1709.             select *
  1710.             from sysprotects p1
  1711.             where
  1712.                 p1.protecttype = 206
  1713.                 and p1.action = p.action
  1714.                 and p1.id = p.id
  1715.                 and p1.uid = u.uid
  1716.                 and case substring(p1.columns, 1, 1) & 1
  1717.                         when NULL then 255  /* all columns have permission */
  1718.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1719.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1720.                     end
  1721.                     & v.high <> 0)          /* permission applies to this column */
  1722.     union
  1723.     select  /*  Add rows for table owner */
  1724.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1725.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1726.         @table_name TABLE_NAME,
  1727.         convert(sysname,col_name(@table_id, c.colid)) COLUMN_NAME,
  1728.         convert(sysname,user_name(u.uid)) grantor,
  1729.         convert(sysname,user_name(o.uid)) grantee,
  1730.         convert (varchar(32),case v.number
  1731.             when 193 then 'SELECT'
  1732.             when 195 then 'INSERT'
  1733.             when 197 then 'UPDATE'
  1734.             else 'REFERENCES'
  1735.         end) PRIVILEGE,
  1736.         convert(varchar(3),'YES') IS_GRANTABLE
  1737.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1738.     where
  1739.         c.id = @table_id
  1740.         and c.name like @column_name
  1741.         and c.id = o.id
  1742.         and u.uid = 1       /* grantor is 'dbo' of database */
  1743.         and v.type = N'P'   /* cross product to get all exposed privileges */
  1744.         and v.number in (26,193,195,197)
  1745.         and not exists (    /* exclude revoke'd privileges */
  1746.             select *
  1747.             from sysprotects p1
  1748.             where
  1749.                 p1.protecttype = 206
  1750.                 and p1.action = v.number
  1751.                 and p1.id = o.id
  1752.                 and p1.uid = o.uid)
  1753.     order by 4, 7
  1754. go
  1755.  
  1756.  
  1757. grant execute on sp_column_privileges to public
  1758. go
  1759.  
  1760. dump tran master with no_log
  1761. go
  1762.  
  1763. print 'creating sp_columns'
  1764. go
  1765.  
  1766. /*  Procedure for pre-6.0 server */
  1767. CREATE PROCEDURE sp_columns (
  1768.                  @table_name        varchar(96),
  1769.                  @table_owner       varchar(96) = null,
  1770.                  @table_qualifier   varchar(32) = null,
  1771.                  @column_name       varchar(96) = null,
  1772.                  @ODBCVer           int = 2)
  1773. AS
  1774.     DECLARE @full_table_name varchar(193)
  1775.     DECLARE @table_id        int
  1776.  
  1777.     if @ODBCVer <> 3
  1778.         select @ODBCVer = 2
  1779.     if @column_name is null /*  If column name not supplied, match all */
  1780.         select @column_name = '%'
  1781.     if @table_qualifier is not null
  1782.     begin
  1783.         if db_name() <> @table_qualifier
  1784.         begin   /* If qualifier doesn't match current database */
  1785.             raiserror 20001 '~~Rush_5~~'
  1786.             return
  1787.         end
  1788.     end
  1789.     if @table_name is null
  1790.     begin   /*  If table name not supplied, match all */
  1791.         select @table_name = '%'
  1792.     end
  1793.     if @table_owner is null
  1794.     begin   /* If unqualified table name */
  1795.         SELECT @full_table_name = @table_name
  1796.     end
  1797.     else
  1798.     begin   /* Qualified table name */
  1799.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1800.     end
  1801.  
  1802.     /*  Get Object ID */
  1803.     SELECT @table_id = object_id(@full_table_name)
  1804.     if ((charindex('%',@full_table_name) = 0) and
  1805.         (charindex('_',@full_table_name) = 0) and
  1806.         @table_id <> 0)
  1807.     begin
  1808.         /* this block is for the case where there is no pattern
  1809.              matching required for the table name */
  1810.  
  1811.         SELECT
  1812.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1813.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1814.             TABLE_NAME = convert(varchar(32),o.name),
  1815.             COLUMN_NAME = convert(varchar(32),c.name),
  1816.             d.DATA_TYPE,
  1817.             TYPE_NAME = t.name,
  1818.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1819.             LENGTH = isnull(d.length, convert(int,c.length)),
  1820.             SCALE = d.numeric_scale,
  1821.             d.RADIX,
  1822.             NULLABLE =  /* set nullability from status flag */
  1823.                 convert(smallint, convert(bit, c.status&8)),
  1824.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  1825.             COLUMN_DEF = text,
  1826.             d.SQL_DATA_TYPE,
  1827.             d.SQL_DATETIME_SUB,
  1828.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1829.             ORDINAL_POSITION = convert(int,
  1830.                        (
  1831.                         select count(*)
  1832.                         from syscolumns sc
  1833.                         where sc.id     =  c.id
  1834.                           AND sc.number =  c.number
  1835.                           AND sc.colid  <= c.colid
  1836.                         )),
  1837.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1838.             SS_DATA_TYPE = c.type
  1839.         FROM
  1840.             syscolumns c,
  1841.             sysobjects o,
  1842.             syscomments m,
  1843.             master.dbo.spt_datatype_info d,
  1844.             systypes t
  1845.         WHERE
  1846.             o.id = @table_id
  1847.             AND c.id = o.id
  1848.             AND t.type = d.ss_dtype
  1849.             AND c.length = isnull(d.fixlen, c.length)
  1850.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1851.             AND o.type <> 'P'
  1852.             AND c.usertype = t.usertype
  1853.             AND c.name like @column_name
  1854.             AND c.cdefault *= m.id
  1855.             AND m.colid = 1
  1856.         ORDER BY 17
  1857.     end
  1858.     else
  1859.     begin
  1860.         /* this block is for the case where there IS pattern
  1861.              matching done on the table name */
  1862.         if @table_owner is null /*  If owner not supplied, match all */
  1863.             select @table_owner = '%'
  1864.  
  1865.         SELECT
  1866.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1867.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1868.             TABLE_NAME = convert(varchar(32),o.name),
  1869.             COLUMN_NAME = convert(varchar(32),c.name),
  1870.             d.DATA_TYPE,
  1871.             TYPE_NAME = t.name,
  1872.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1873.             LENGTH = isnull(d.length, convert(int,c.length)),
  1874.             SCALE = d.numeric_scale,
  1875.             d.RADIX,
  1876.             NULLABLE =  /* set nullability from status flag */
  1877.                 convert(smallint, convert(bit, c.status&8)),
  1878.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  1879.             COLUMN_DEF = text,
  1880.             d.SQL_DATA_TYPE,
  1881.             d.SQL_DATETIME_SUB,
  1882.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1883.             ORDINAL_POSITION = convert(int,
  1884.                        (
  1885.                         select count(*)
  1886.                         from syscolumns sc
  1887.                         where sc.id     =  c.id
  1888.                           AND sc.number =  c.number
  1889.                           AND sc.colid  <= c.colid
  1890.                         )),
  1891.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1892.             SS_DATA_TYPE = c.type
  1893.         FROM
  1894.             syscolumns c,
  1895.             sysobjects o,
  1896.             syscomments m,
  1897.             master.dbo.spt_datatype_info d,
  1898.             systypes t
  1899.         WHERE
  1900.             o.name like @table_name
  1901.             AND user_name(o.uid) like @table_owner
  1902.             AND o.id = c.id
  1903.             AND t.type = d.ss_dtype
  1904.             AND c.length = isnull(d.fixlen, c.length)
  1905.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1906.             AND o.type <> 'P'
  1907.             AND c.usertype = t.usertype
  1908.             AND c.name like @column_name
  1909.             AND c.cdefault *= m.id
  1910.             AND m.colid = 1
  1911.         ORDER BY 2, 3, 17
  1912.     end
  1913. go
  1914.  
  1915. if (charindex('6.00', @@version) = 0 and
  1916.     charindex('6.50', @@version) = 0 and
  1917.     charindex('7.00', @@version) = 0 and
  1918.     charindex('8.00', @@version) = 0)
  1919. begin
  1920.     print ''
  1921.     print ''
  1922.     print 'Warning:'
  1923.     print 'you are installing the stored procedures '
  1924.     print 'on a pre 6.0 SQL Server.'
  1925.     print 'Ignore the following error.'
  1926. end
  1927. else
  1928.     drop proc sp_columns
  1929. go
  1930.  
  1931. /*  Procedure for 6.0 and 6.50 server */
  1932. CREATE PROCEDURE sp_columns (
  1933.                  @table_name        varchar(96),
  1934.                  @table_owner       varchar(96) = null,
  1935.                  @table_qualifier   varchar(32) = null,
  1936.                  @column_name       varchar(96) = null,
  1937.                  @ODBCVer           int = 2)
  1938. AS
  1939.     DECLARE @full_table_name    varchar(193)
  1940.     DECLARE @table_id int
  1941.  
  1942.     if @ODBCVer <> 3
  1943.         select @ODBCVer = 2
  1944.     if @column_name is null /*  If column name not supplied, match all */
  1945.         select @column_name = '%'
  1946.     if @table_qualifier is not null
  1947.     begin
  1948.         if db_name() <> @table_qualifier
  1949.         begin   /* If qualifier doesn't match current database */
  1950.             raiserror (15250, -1,-1)
  1951.             return
  1952.         end
  1953.     end
  1954.     if @table_name is null
  1955.     begin
  1956.         /* If table name not supplied, match all */
  1957.         select @table_name = '%'
  1958.     end
  1959.     if @table_owner is null
  1960.     begin
  1961.         /* If unqualified table name */
  1962.         SELECT @full_table_name = @table_name
  1963.     end
  1964.     else
  1965.     begin
  1966.         /* Qualified table name */
  1967.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1968.     end
  1969.  
  1970.     /* Get Object ID */
  1971.     SELECT @table_id = object_id(@full_table_name)
  1972.     if ((charindex('%',@full_table_name) = 0) and
  1973.         (charindex('[',@full_table_name) = 0) and
  1974.         (charindex('_',@full_table_name) = 0) and
  1975.         @table_id <> 0)
  1976.     begin
  1977.         /* this block is for the case where there is no pattern
  1978.             matching required for the table name */
  1979.  
  1980.         SELECT
  1981.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1982.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1983.             TABLE_NAME = convert(varchar(32),o.name),
  1984.             COLUMN_NAME = convert(varchar(32),c.name),
  1985.             DATA_TYPE = d.DATA_TYPE,
  1986.             TYPE_NAME = convert(varchar(32),
  1987.                     case
  1988.                         when t.usertype > 100 or t.usertype in (18,80) then t.name
  1989.                         else d.TYPE_NAME
  1990.                     end),
  1991.             "PRECISION" = convert(int,
  1992.                     case
  1993.                         when d.DATA_TYPE in (6,7) then d.data_precision /* FLOAT/REAL */
  1994.                         else isnull(convert(int,c.prec), 2147483647)
  1995.                     end),
  1996.             LENGTH = convert(int,
  1997.                     case
  1998.                         when d.ss_dtype IN (106, 108, 55, 63) then  /* decimal/numeric types */
  1999.                         c.prec+2
  2000.                         else
  2001.                             isnull(d.length, c.length)
  2002.                     end),
  2003.             SCALE = convert(smallint, c.scale),
  2004.             RADIX = d.RADIX,
  2005.             NULLABLE =  /* set nullability from status flag */
  2006.                 convert(smallint, convert(bit, c.status&8)),
  2007.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  2008.             COLUMN_DEF = text,
  2009.             SQL_DATA_TYPE = d.SQL_DATA_TYPE,
  2010.             SQL_DATETIME_SUB = d.SQL_DATETIME_SUB,
  2011.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  2012.             ORDINAL_POSITION = convert(int,
  2013.                        (
  2014.                         select count(*)
  2015.                         from syscolumns sc
  2016.                         where sc.id     =  c.id
  2017.                           AND sc.number =  c.number
  2018.                           AND sc.colid  <= c.colid
  2019.                         )),
  2020.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  2021.             SS_DATA_TYPE = c.type
  2022.         FROM
  2023.             syscolumns c,
  2024.             sysobjects o,
  2025.             syscomments m,
  2026.             master.dbo.spt_datatype_info d,
  2027.             systypes t
  2028.         WHERE
  2029.             o.id = @table_id
  2030.             AND c.id = o.id
  2031.             AND t.type = d.ss_dtype
  2032.             AND c.length = isnull(d.fixlen, c.length)
  2033.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2034.             AND o.type <> 'P'
  2035.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  2036.             AND c.usertype = t.usertype
  2037.             AND c.name like @column_name
  2038.             AND c.cdefault *= m.id
  2039.             AND m.colid = 1
  2040.         ORDER BY 17
  2041.     end
  2042.     else
  2043.     begin
  2044.         /* this block is for the case where there IS pattern
  2045.             matching done on the table name */
  2046.  
  2047.         if @table_owner is null /*  If owner not supplied, match all */
  2048.             select @table_owner = '%'
  2049.  
  2050.         SELECT
  2051.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  2052.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  2053.             TABLE_NAME = convert(varchar(32),o.name),
  2054.             COLUMN_NAME = convert(varchar(32),c.name),
  2055.             DATA_TYPE = d.DATA_TYPE,
  2056.             TYPE_NAME = convert(varchar(32), 
  2057.                 case 
  2058.                     when t.usertype > 100 or t.usertype in (18,80) then t.name
  2059.                     else d.TYPE_NAME
  2060.                 end),
  2061.             "PRECISION" = convert(int,
  2062.                     case
  2063.                         when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2064.                         else isnull(convert(int,c.prec), 2147483647)
  2065.                     end),
  2066.             LENGTH = convert(int,
  2067.                     case
  2068.                         when d.ss_dtype IN (106, 108, 55, 63) then  /* decimal/numeric types */
  2069.                             c.prec+2
  2070.                         else
  2071.                             isnull(d.length, c.length)
  2072.                     end),
  2073.             SCALE = convert(smallint, c.scale),
  2074.             RADIX = d.RADIX,
  2075.             NULLABLE =  /* set nullability from status flag */
  2076.                 convert(smallint, convert(bit, c.status&8)),
  2077.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  2078.             COLUMN_DEF = text,
  2079.             SQL_DATA_TYPE = d.SQL_DATA_TYPE,
  2080.             SQL_DATETIME_SUB = d.SQL_DATETIME_SUB,
  2081.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647) + d.charbin,
  2082.             ORDINAL_POSITION = convert(int,
  2083.                        (
  2084.                         select count(*)
  2085.                         from syscolumns sc
  2086.                         where sc.id     =  c.id
  2087.                           AND sc.number =  c.number
  2088.                           AND sc.colid  <= c.colid
  2089.                         )),
  2090.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  2091.             SS_DATA_TYPE = c.type
  2092.         FROM
  2093.             syscolumns c,
  2094.             sysobjects o,
  2095.             syscomments m,
  2096.             master.dbo.spt_datatype_info d,
  2097.             systypes t
  2098.         WHERE
  2099.             o.name like @table_name
  2100.             AND user_name(o.uid) like @table_owner
  2101.             AND o.id = c.id
  2102.             AND t.type = d.ss_dtype
  2103.             AND c.length = isnull(d.fixlen, c.length)
  2104.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2105.             AND o.type <> 'P'
  2106.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  2107.             AND c.usertype = t.usertype
  2108.             AND c.name like @column_name
  2109.             AND c.cdefault *= m.id
  2110.             AND m.colid = 1
  2111.         ORDER BY 2, 3, 17
  2112.     end
  2113. go
  2114.  
  2115. if (charindex('7.00', @@version) = 0 and
  2116.     charindex('8.00', @@version) = 0)
  2117. begin
  2118.     print ''
  2119.     print ''
  2120.     print 'Warning:'
  2121.     print 'you are installing the stored procedures '
  2122.     print 'on a pre 7.0 SQL Server.'
  2123.     print 'Ignore the following errors.'
  2124. end
  2125. else
  2126.     drop proc sp_columns
  2127. go
  2128.  
  2129. /*  Procedure for 7.0 server */
  2130. CREATE PROCEDURE sp_columns (
  2131.                  @table_name        nvarchar(384),
  2132.                  @table_owner       nvarchar(384) = null,
  2133.                  @table_qualifier   sysname = null,
  2134.                  @column_name       nvarchar(384) = null,
  2135.                  @ODBCVer           int = 2)
  2136. AS
  2137.     DECLARE @full_table_name    nvarchar(769)
  2138.     DECLARE @table_id int
  2139.  
  2140.     if @ODBCVer <> 3
  2141.         select @ODBCVer = 2
  2142.     if @column_name is null /*  If column name not supplied, match all */
  2143.         select @column_name = '%'
  2144.     if @table_qualifier is not null
  2145.     begin
  2146.         if db_name() <> @table_qualifier
  2147.         begin   /* If qualifier doesn't match current database */
  2148.             raiserror (15250, -1,-1)
  2149.             return
  2150.         end
  2151.     end
  2152.     if @table_name is null
  2153.     begin   /*  If table name not supplied, match all */
  2154.         select @table_name = '%'
  2155.     end
  2156.     if @table_owner is null
  2157.     begin   /* If unqualified table name */
  2158.         SELECT @full_table_name = quotename(@table_name)
  2159.     end
  2160.     else
  2161.     begin   /* Qualified table name */
  2162.         if @table_owner = ''
  2163.         begin   /* If empty owner name */
  2164.             SELECT @full_table_name = quotename(@table_owner)
  2165.         end
  2166.         else
  2167.         begin
  2168.             SELECT @full_table_name = quotename(@table_owner) +
  2169.                 '.' + quotename(@table_name)
  2170.         end
  2171.     end
  2172.  
  2173.     /*  Get Object ID */
  2174.     SELECT @table_id = object_id(@full_table_name)
  2175.     if ((isnull(charindex('%', @full_table_name),0) = 0) and
  2176.         (isnull(charindex('[', @table_name),0) = 0) and
  2177.         (isnull(charindex('[', @table_owner),0) = 0) and
  2178.         (isnull(charindex('_', @full_table_name),0) = 0) and
  2179.         @table_id <> 0)
  2180.     begin
  2181.         /* this block is for the case where there is no pattern
  2182.             matching required for the table name */
  2183.         
  2184.         SELECT
  2185.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2186.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2187.             TABLE_NAME = convert(sysname,o.name),
  2188.             COLUMN_NAME = convert(sysname,c.name),
  2189.             d.DATA_TYPE,
  2190.             convert (sysname,case
  2191.                 when t.xusertype > 255 then t.name
  2192.                 else d.TYPE_NAME
  2193.             end) TYPE_NAME,
  2194.             convert(int,case
  2195.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2196.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2197.             end) "PRECISION",
  2198.             convert(int,case
  2199.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2200.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2201.                 else
  2202.                     isnull(d.length, c.length)
  2203.             end) LENGTH,
  2204.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2205.             d.RADIX,
  2206.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2207.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  2208.             COLUMN_DEF = text,
  2209.             d.SQL_DATA_TYPE,
  2210.             d.SQL_DATETIME_SUB,
  2211.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2212.             ORDINAL_POSITION = convert(int,
  2213.                        (
  2214.                         select count(*)
  2215.                         from syscolumns sc
  2216.                         where sc.id     =  c.id
  2217.                           AND sc.number =  c.number
  2218.                           AND sc.colid  <= c.colid
  2219.                         )),
  2220.             IS_NULLABLE = convert(varchar(254),
  2221.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  2222.             SS_DATA_TYPE = c.type
  2223.         FROM
  2224.             sysobjects o,
  2225.             master.dbo.spt_datatype_info d,
  2226.             systypes t,
  2227.             syscolumns c
  2228.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2229.                 AND m.colid = 1
  2230.         WHERE
  2231.             o.id = @table_id
  2232.             AND c.id = o.id
  2233.             AND t.xtype = d.ss_dtype
  2234.             AND c.length = isnull(d.fixlen, c.length)
  2235.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2236.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2237.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2238.             AND c.xusertype = t.xusertype
  2239.             AND c.name like @column_name
  2240.         ORDER BY 17
  2241.     end
  2242.     else
  2243.     begin
  2244.         /* this block is for the case where there IS pattern
  2245.             matching done on the table name */
  2246.  
  2247.         if @table_owner is null /*  If owner not supplied, match all */
  2248.             select @table_owner = '%'
  2249.  
  2250.         SELECT
  2251.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2252.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2253.             TABLE_NAME = convert(sysname,o.name),
  2254.             COLUMN_NAME = convert(sysname,c.name),
  2255.             d.DATA_TYPE,
  2256.             convert (sysname,case
  2257.                 when t.xusertype > 255 then t.name
  2258.                 else d.TYPE_NAME
  2259.             end) TYPE_NAME,
  2260.             convert(int,case
  2261.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2262.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2263.             end) "PRECISION",
  2264.             convert(int,case
  2265.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2266.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2267.                 else
  2268.                     isnull(d.length, c.length)
  2269.             end) LENGTH,
  2270.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2271.             d.RADIX,
  2272.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2273.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  2274.             COLUMN_DEF = text,
  2275.             d.SQL_DATA_TYPE,
  2276.             d.SQL_DATETIME_SUB,
  2277.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2278.             ORDINAL_POSITION = convert(int,
  2279.                        (
  2280.                         select count(*)
  2281.                         from syscolumns sc
  2282.                         where sc.id     =  c.id
  2283.                           AND sc.number =  c.number
  2284.                           AND sc.colid  <= c.colid
  2285.                         )),
  2286.             IS_NULLABLE = convert(varchar(254),
  2287.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  2288.             SS_DATA_TYPE = c.type
  2289.         FROM
  2290.             sysobjects o,
  2291.             master.dbo.spt_datatype_info d,
  2292.             systypes t,
  2293.             syscolumns c
  2294.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2295.                 AND m.colid = 1
  2296.         WHERE
  2297.             o.name like @table_name
  2298.             AND user_name(o.uid) like @table_owner
  2299.             AND o.id = c.id
  2300.             AND t.xtype = d.ss_dtype
  2301.             AND c.length = isnull(d.fixlen, c.length)
  2302.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2303.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2304.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2305.             AND c.xusertype = t.xusertype
  2306.             AND c.name like @column_name
  2307.         ORDER BY 2, 3, 17
  2308.     end
  2309. go
  2310.  
  2311. if (charindex('8.00', @@version) = 0)
  2312. begin
  2313.     print ''
  2314.     print ''
  2315.     print 'Warning:'
  2316.     print 'you are installing the stored procedures '
  2317.     print 'on a pre 8.0 SQL Server.'
  2318.     print 'Ignore the following errors.'
  2319. end
  2320. else
  2321.     drop proc sp_columns
  2322. go
  2323.  
  2324. /*  Procedure for 8.0 server */
  2325. CREATE PROCEDURE sp_columns (
  2326.                  @table_name        nvarchar(384),
  2327.                  @table_owner       nvarchar(384) = null,
  2328.                  @table_qualifier   sysname = null,
  2329.                  @column_name       nvarchar(384) = null,
  2330.                  @ODBCVer           int = 2)
  2331. AS
  2332.     DECLARE @full_table_name    nvarchar(769)
  2333.     DECLARE @table_id int
  2334.  
  2335.     if @ODBCVer <> 3
  2336.         select @ODBCVer = 2
  2337.     if @column_name is null /*  If column name not supplied, match all */
  2338.         select @column_name = '%'
  2339.     if @table_qualifier is not null
  2340.     begin
  2341.         if db_name() <> @table_qualifier
  2342.         begin   /* If qualifier doesn't match current database */
  2343.             raiserror (15250, -1,-1)
  2344.             return
  2345.         end
  2346.     end
  2347.     if @table_name is null
  2348.     begin   /*  If table name not supplied, match all */
  2349.         select @table_name = '%'
  2350.     end
  2351.     if @table_owner is null
  2352.     begin   /* If unqualified table name */
  2353.         SELECT @full_table_name = quotename(@table_name)
  2354.     end
  2355.     else
  2356.     begin   /* Qualified table name */
  2357.         if @table_owner = ''
  2358.         begin   /* If empty owner name */
  2359.             SELECT @full_table_name = quotename(@table_owner)
  2360.         end
  2361.         else
  2362.         begin
  2363.             SELECT @full_table_name = quotename(@table_owner) +
  2364.                 '.' + quotename(@table_name)
  2365.         end
  2366.     end
  2367.  
  2368.     /*  Get Object ID */
  2369.     SELECT @table_id = object_id(@full_table_name)
  2370.     if ((isnull(charindex('%', @full_table_name),0) = 0) and
  2371.         (isnull(charindex('[', @table_name),0) = 0) and
  2372.         (isnull(charindex('[', @table_owner),0) = 0) and
  2373.         (isnull(charindex('_', @full_table_name),0) = 0) and
  2374.         @table_id <> 0)
  2375.     begin
  2376.         /* this block is for the case where there is no pattern
  2377.             matching required for the table name */
  2378.         
  2379.         SELECT
  2380.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2381.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2382.             TABLE_NAME = convert(sysname,o.name),
  2383.             COLUMN_NAME = convert(sysname,c.name),
  2384.             d.DATA_TYPE,
  2385.             convert (sysname,case
  2386.                 when t.xusertype > 255 then t.name
  2387.                 else d.TYPE_NAME collate database_default
  2388.             end) TYPE_NAME,
  2389.             convert(int,case
  2390.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2391.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2392.             end) "PRECISION",
  2393.             convert(int,case
  2394.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2395.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2396.                 else
  2397.                     isnull(d.length, c.length)
  2398.             end) LENGTH,
  2399.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2400.             d.RADIX,
  2401.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2402.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  2403.             COLUMN_DEF = text,
  2404.             d.SQL_DATA_TYPE,
  2405.             d.SQL_DATETIME_SUB,
  2406.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2407.             ORDINAL_POSITION = convert(int,
  2408.                        (
  2409.                         select count(*)
  2410.                         from syscolumns sc
  2411.                         where sc.id     =  c.id
  2412.                           AND sc.number =  c.number
  2413.                           AND sc.colid  <= c.colid
  2414.                         )),
  2415.             IS_NULLABLE = convert(varchar(254),
  2416.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  2417.             SS_DATA_TYPE = c.type
  2418.         FROM
  2419.             sysobjects o,
  2420.             master.dbo.spt_datatype_info d,
  2421.             systypes t,
  2422.             syscolumns c
  2423.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2424.                 AND m.colid = 1
  2425.         WHERE
  2426.             o.id = @table_id
  2427.             AND c.id = o.id
  2428.             AND t.xtype = d.ss_dtype
  2429.             AND c.length = isnull(d.fixlen, c.length)
  2430.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2431.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2432.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2433.             AND c.xusertype = t.xusertype
  2434.             AND c.name like @column_name
  2435.         ORDER BY 17
  2436.     end
  2437.     else
  2438.     begin
  2439.         /* this block is for the case where there IS pattern
  2440.             matching done on the table name */
  2441.  
  2442.         if @table_owner is null /*  If owner not supplied, match all */
  2443.             select @table_owner = '%'
  2444.  
  2445.         SELECT
  2446.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2447.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2448.             TABLE_NAME = convert(sysname,o.name),
  2449.             COLUMN_NAME = convert(sysname,c.name),
  2450.             d.DATA_TYPE,
  2451.             convert (sysname,case
  2452.                 when t.xusertype > 255 then t.name
  2453.                 else d.TYPE_NAME collate database_default
  2454.             end) TYPE_NAME,
  2455.             convert(int,case
  2456.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2457.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2458.             end) "PRECISION",
  2459.             convert(int,case
  2460.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2461.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2462.                 else
  2463.                     isnull(d.length, c.length)
  2464.             end) LENGTH,
  2465.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2466.             d.RADIX,
  2467.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2468.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  2469.             COLUMN_DEF = text,
  2470.             d.SQL_DATA_TYPE,
  2471.             d.SQL_DATETIME_SUB,
  2472.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2473.             ORDINAL_POSITION = convert(int,
  2474.                        (
  2475.                         select count(*)
  2476.                         from syscolumns sc
  2477.                         where sc.id     =  c.id
  2478.                           AND sc.number =  c.number
  2479.                           AND sc.colid  <= c.colid
  2480.                         )),
  2481.             IS_NULLABLE = convert(varchar(254),
  2482.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  2483.             SS_DATA_TYPE = c.type
  2484.         FROM
  2485.             sysobjects o,
  2486.             master.dbo.spt_datatype_info d,
  2487.             systypes t,
  2488.             syscolumns c
  2489.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2490.                 AND m.colid = 1
  2491.         WHERE
  2492.             o.name like @table_name
  2493.             AND user_name(o.uid) like @table_owner
  2494.             AND o.id = c.id
  2495.             AND t.xtype = d.ss_dtype
  2496.             AND c.length = isnull(d.fixlen, c.length)
  2497.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2498.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2499.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2500.             AND c.xusertype = t.xusertype
  2501.             AND c.name like @column_name
  2502.         ORDER BY 2, 3, 17
  2503.     end
  2504. go
  2505.  
  2506. grant execute on sp_columns to public
  2507. go
  2508.  
  2509. dump tran master with no_log
  2510. go
  2511.  
  2512. print 'creating sp_databases'
  2513. go
  2514.  
  2515. /*  Procedure for pre-7.0 server */
  2516. create proc sp_databases
  2517. as
  2518.     set nocount on
  2519.     /* Use temporary table to sum up database size w/o using group by */
  2520.     create table #databases (
  2521.                   DATABASE_NAME varchar(32) NOT NULL,
  2522.                   size int NOT NULL)
  2523.  
  2524.     /* Insert row for each database */
  2525.     insert into #databases
  2526.         select
  2527.             name,
  2528.             (select sum(size) from master.dbo.sysusages
  2529.                 where dbid = d.dbid)
  2530.         from master.dbo.sysdatabases d
  2531.  
  2532.     select
  2533.          DATABASE_NAME,
  2534.          DATABASE_SIZE = size*2,    /* Convert from 2048 byte pages to K */
  2535.          REMARKS = convert(varchar(254),null)   /* Remarks are NULL */
  2536.     from #databases
  2537.     order by 1
  2538. go
  2539.  
  2540. if (charindex('7.00', @@version) = 0 and
  2541.     charindex('8.00', @@version) = 0)
  2542. begin
  2543.     print ''
  2544.     print ''
  2545.     print 'Warning:'
  2546.     print 'you are installing the stored procedures '
  2547.     print 'on a pre 8.0 SQL Server.'
  2548.     print 'Ignore the following errors.'
  2549. end
  2550. else
  2551.     drop proc sp_databases
  2552. go
  2553.  
  2554. /*  Procedure for 8.0 server */
  2555. create proc sp_databases
  2556. as
  2557.     set nocount on
  2558.     declare @name sysname
  2559.     declare @SQL  nvarchar(600)
  2560.  
  2561.     /* Use temporary table to sum up database size w/o using group by */
  2562.     create table #databases (
  2563.                   DATABASE_NAME sysname NOT NULL,
  2564.                   size int NOT NULL)
  2565.  
  2566.     declare c1 cursor for 
  2567.         select name from master.dbo.sysdatabases
  2568.             where has_dbaccess(name) = 1 -- Only look at databases to which we have access
  2569.  
  2570.     open c1
  2571.     fetch c1 into @name
  2572.  
  2573.     while @@fetch_status >= 0
  2574.     begin
  2575.         select @SQL = 'insert into #databases
  2576.                 select N'''+ @name + ''', sum(size) from '
  2577.                 + QuoteName(@name) + '.dbo.sysfiles'
  2578.         /* Insert row for each database */
  2579.         execute (@SQL)
  2580.         fetch c1 into @name
  2581.     end
  2582.     deallocate c1
  2583.  
  2584.     select  
  2585.         DATABASE_NAME,
  2586.         DATABASE_SIZE = size*8,/* Convert from 8192 byte pages to K */
  2587.         REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2588.     from #databases
  2589.     order by 1
  2590. go
  2591.  
  2592. grant execute on sp_databases to public
  2593. go
  2594.  
  2595. dump tran master with no_log
  2596. go
  2597.  
  2598. print 'creating sp_datatype_info'
  2599. go
  2600.  
  2601. /*  Procedure for pre-6.0 server */
  2602. create proc sp_datatype_info
  2603.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2604. as
  2605.     if @ODBCVer <> 3
  2606.         select @ODBCVer = 2
  2607.     if @data_type = 0
  2608.         select
  2609.             TYPE_NAME = t.name,
  2610.             d.DATA_TYPE,
  2611.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2612.             d.LITERAL_PREFIX,
  2613.             d.LITERAL_SUFFIX,
  2614.             e.CREATE_PARAMS,
  2615.             d.NULLABLE,
  2616.             d.CASE_SENSITIVE,
  2617.             d.SEARCHABLE,
  2618.             d.UNSIGNED_ATTRIBUTE,
  2619.             d.MONEY,
  2620.             d.AUTO_INCREMENT,
  2621.             LOCAL_TYPE_NAME = t.name,
  2622.             MINIMUM_SCALE = d.numeric_scale,
  2623.             MAXIMUM_SCALE = d.numeric_scale,
  2624.             d.SQL_DATA_TYPE,
  2625.             d.SQL_DATETIME_SUB,
  2626.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2627.             INTERVAL_PRECISION = convert(smallint,NULL),
  2628.             USERTYPE = t.usertype
  2629.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2630.         where
  2631.             d.ss_dtype = t.type
  2632.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2633.             and t.usertype *= e.user_type
  2634.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2635.                 t.usertype > 100)
  2636.         order by 2, 12, 11, t.usertype
  2637.  
  2638.     else
  2639.         select
  2640.             TYPE_NAME = t.name,
  2641.             d.DATA_TYPE,
  2642.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2643.             d.LITERAL_PREFIX,
  2644.             d.LITERAL_SUFFIX,
  2645.             e.CREATE_PARAMS,
  2646.             d.NULLABLE,
  2647.             d.CASE_SENSITIVE,
  2648.             d.SEARCHABLE,
  2649.             d.UNSIGNED_ATTRIBUTE,
  2650.             d.MONEY,
  2651.             d.AUTO_INCREMENT,
  2652.             LOCAL_TYPE_NAME = t.name,
  2653.             MINIMUM_SCALE = d.numeric_scale,
  2654.             MAXIMUM_SCALE = d.numeric_scale,
  2655.             d.SQL_DATA_TYPE,
  2656.             d.SQL_DATETIME_SUB,
  2657.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2658.             INTERVAL_PRECISION = convert(smallint,NULL),
  2659.             USERTYPE = t.usertype
  2660.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2661.         where
  2662.             DATA_TYPE = @data_type
  2663.             and d.ss_dtype = t.type
  2664.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2665.             and t.usertype *= e.user_type
  2666.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2667.                 t.usertype > 100)
  2668.         order by 12, 11, t.usertype
  2669.  
  2670. go
  2671.  
  2672. if (charindex('6.00', @@version) = 0 and
  2673.     charindex('6.50', @@version) = 0 and
  2674.     charindex('7.00', @@version) = 0 and
  2675.     charindex('8.00', @@version) = 0)
  2676. begin
  2677.     print ''
  2678.     print ''
  2679.     print 'Warning:'
  2680.     print 'you are installing the stored procedures '
  2681.     print 'on a pre 6.0 SQL Server.'
  2682.     print 'Ignore the following errors.'
  2683. end
  2684. else
  2685.     drop proc sp_datatype_info
  2686. go
  2687.  
  2688. /*  Procedure for 6.0 and 6.50 servers */
  2689. create proc sp_datatype_info
  2690.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2691. as
  2692.     if @ODBCVer <> 3
  2693.         select @ODBCVer = 2
  2694.     if @data_type = 0
  2695.         select
  2696.             convert(varchar(32),case
  2697.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2698.                 else d.TYPE_NAME
  2699.             end) TYPE_NAME,
  2700.             d.DATA_TYPE,
  2701.             convert(int,case
  2702.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2703.                 when d.ss_dtype in (35,34) then 2147483647              /* TEXT/IMAGE */
  2704.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2705.                 else t.prec
  2706.             end) "PRECISION",
  2707.             d.LITERAL_PREFIX,
  2708.             d.LITERAL_SUFFIX,
  2709.             e.CREATE_PARAMS,
  2710.             convert(smallint,case
  2711.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2712.                 else t.allownulls
  2713.             end) NULLABLE,
  2714.             d.CASE_SENSITIVE,
  2715.             d.SEARCHABLE,
  2716.             d.UNSIGNED_ATTRIBUTE,
  2717.             d.MONEY,
  2718.             d.AUTO_INCREMENT,
  2719.             convert(varchar(32),case
  2720.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2721.                 else d.TYPE_NAME
  2722.             end) LOCAL_TYPE_NAME,
  2723.             convert(smallint,case
  2724.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2725.                 else d.numeric_scale
  2726.             end) MINIMUM_SCALE,
  2727.             convert(smallint,case
  2728.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2729.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2730.                 else t.scale
  2731.             end) MAXIMUM_SCALE,
  2732.             d.SQL_DATA_TYPE,
  2733.             d.SQL_DATETIME_SUB,
  2734.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2735.             INTERVAL_PRECISION = convert(smallint,NULL),
  2736.             USERTYPE = t.usertype
  2737.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2738.         where
  2739.             d.ss_dtype = t.type
  2740.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2741.             and t.usertype *= e.user_type
  2742.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2743.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2744.                 t.usertype > 100)
  2745.             and (t.usertype <= 100 or
  2746.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2747.         order by 2, 12, 11, t.usertype
  2748.  
  2749.     else
  2750.         select
  2751.             convert(varchar(32),case
  2752.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2753.                 else d.TYPE_NAME
  2754.             end) TYPE_NAME,
  2755.             d.DATA_TYPE,
  2756.             convert(int,case
  2757.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2758.                 when d.ss_dtype in (35,34) then 2147483647              /* TEXT/IMAGE */
  2759.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2760.                 else t.prec
  2761.             end) "PRECISION",
  2762.             d.LITERAL_PREFIX,
  2763.             d.LITERAL_SUFFIX,
  2764.             e.CREATE_PARAMS,
  2765.             convert(smallint,case
  2766.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2767.                 else t.allownulls
  2768.             end) NULLABLE,
  2769.             d.CASE_SENSITIVE,
  2770.             d.SEARCHABLE,
  2771.             d.UNSIGNED_ATTRIBUTE,
  2772.             d.MONEY,
  2773.             d.AUTO_INCREMENT,
  2774.             convert(varchar(32),case
  2775.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2776.                 else d.TYPE_NAME
  2777.             end) LOCAL_TYPE_NAME,
  2778.             convert(smallint,case
  2779.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2780.                 else d.numeric_scale
  2781.             end) MINIMUM_SCALE,
  2782.             convert(smallint,case
  2783.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2784.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2785.                 else t.scale
  2786.             end) MAXIMUM_SCALE,
  2787.             d.SQL_DATA_TYPE,
  2788.             d.SQL_DATETIME_SUB,
  2789.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2790.             INTERVAL_PRECISION = convert(smallint,NULL),
  2791.             USERTYPE = t.usertype
  2792.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2793.         where
  2794.             d.DATA_TYPE = @data_type
  2795.             and d.ss_dtype = t.type
  2796.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2797.             and t.usertype *= e.user_type
  2798.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2799.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2800.                 t.usertype > 100)
  2801.             and (t.usertype <= 100 or
  2802.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2803.         order by 12, 11, t.usertype
  2804. go
  2805.  
  2806. if (charindex('7.00', @@version) = 0 and
  2807.     charindex('8.00', @@version) = 0)
  2808. begin
  2809.     print ''
  2810.     print ''
  2811.     print 'Warning:'
  2812.     print 'you are installing the stored procedures '
  2813.     print 'on a pre 7.0 SQL Server.'
  2814.     print 'Ignore the following errors.'
  2815. end
  2816. else
  2817.     drop proc sp_datatype_info
  2818. go
  2819.  
  2820. /*  Procedure for 7.0 server */
  2821. create proc sp_datatype_info
  2822.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2823. as
  2824.     declare @mintype int
  2825.     declare @maxtype int
  2826.  
  2827.     if @ODBCVer <> 3
  2828.         select @ODBCVer = 2
  2829.     if @data_type = 0
  2830.     begin
  2831.         select @mintype = -32768
  2832.         select @maxtype = 32767
  2833.     end
  2834.     else
  2835.     begin
  2836.         select @mintype = @data_type
  2837.         select @maxtype = @data_type
  2838.     end
  2839.  
  2840.     select
  2841.         convert(sysname,case
  2842.             when t.xusertype > 255 then t.name
  2843.             else d.TYPE_NAME
  2844.         end) TYPE_NAME,
  2845.         d.DATA_TYPE,
  2846.         convert(int,case
  2847.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2848.             when type_name(d.ss_dtype) IN ('numeric','decimal') and
  2849.                 t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2850.             else OdbcPrec(t.xtype, t.length, t.xprec)
  2851.         end) "PRECISION",
  2852.         d.LITERAL_PREFIX,
  2853.         d.LITERAL_SUFFIX,
  2854.         e.CREATE_PARAMS,
  2855.         convert(smallint,case
  2856.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2857.             else TypeProperty (t.name, 'AllowsNull')
  2858.         end) NULLABLE,
  2859.         d.CASE_SENSITIVE,
  2860.         d.SEARCHABLE,
  2861.         d.UNSIGNED_ATTRIBUTE,
  2862.         d.MONEY,
  2863.         d.AUTO_INCREMENT,
  2864.         convert(sysname,case
  2865.             when t.xusertype > 255 then t.name
  2866.             else d.LOCAL_TYPE_NAME
  2867.         end) LOCAL_TYPE_NAME,
  2868.         MINIMUM_SCALE = d.numeric_scale,
  2869.         convert(smallint,case
  2870.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 0 and t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2871.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2872.             else TypeProperty (t.name, 'Scale')
  2873.         end) MAXIMUM_SCALE,
  2874.         d.SQL_DATA_TYPE,
  2875.         d.SQL_DATETIME_SUB,
  2876.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2877.         INTERVAL_PRECISION = convert(smallint,NULL),
  2878.         USERTYPE = t.usertype
  2879.     from master.dbo.spt_datatype_info d
  2880.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2881.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2882.             t.xusertype = e.user_type
  2883.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2884.     where
  2885.         d.DATA_TYPE between @mintype and @maxtype
  2886.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2887.         and (t.xusertype <= 255 or
  2888.             isnull(d.AUTO_INCREMENT,0) = 0)
  2889.     order by 2, 12, 11,
  2890.     case
  2891.         when t.usertype=18 then 255
  2892.         else t.usertype
  2893.     end
  2894.  
  2895. go
  2896.  
  2897. if (charindex('8.00', @@version) = 0)
  2898. begin
  2899.     print ''
  2900.     print ''
  2901.     print 'Warning:'
  2902.     print 'you are installing the stored procedures '
  2903.     print 'on a pre 8.0 SQL Server.'
  2904.     print 'Ignore the following errors.'
  2905. end
  2906. else
  2907.     drop proc sp_datatype_info
  2908. go
  2909.  
  2910. /*  Procedure for 8.0 server */
  2911. create proc sp_datatype_info
  2912.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2913. as
  2914.     declare @mintype int
  2915.     declare @maxtype int
  2916.  
  2917.     if @ODBCVer <> 3
  2918.         select @ODBCVer = 2
  2919.     if @data_type = 0
  2920.     begin
  2921.         select @mintype = -32768
  2922.         select @maxtype = 32767
  2923.     end
  2924.     else
  2925.     begin
  2926.         select @mintype = @data_type
  2927.         select @maxtype = @data_type
  2928.     end
  2929.  
  2930.     select
  2931.         convert(sysname,case
  2932.             when t.xusertype > 255 then t.name
  2933.             else d.TYPE_NAME collate database_default
  2934.         end) TYPE_NAME,
  2935.         d.DATA_TYPE,
  2936.         convert(int,case
  2937.             when d.DATA_TYPE in (6,7,-150) then d.data_precision        /* FLOAT/REAL/sql_variant*/
  2938.             when d.ss_dtype in (55,63,106,108) and
  2939.                 t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2940.             else OdbcPrec(t.xtype, t.length, t.xprec)
  2941.         end) "PRECISION",
  2942.         d.LITERAL_PREFIX,
  2943.         d.LITERAL_SUFFIX,
  2944.         e.CREATE_PARAMS,
  2945.         convert(smallint,case
  2946.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2947.             else TypeProperty (t.name, 'AllowsNull')
  2948.         end) NULLABLE,
  2949.         d.CASE_SENSITIVE,
  2950.         d.SEARCHABLE,
  2951.         d.UNSIGNED_ATTRIBUTE,
  2952.         d.MONEY,
  2953.         d.AUTO_INCREMENT,
  2954.         convert(sysname,case
  2955.             when t.xusertype > 255 then t.name
  2956.             else d.LOCAL_TYPE_NAME collate database_default
  2957.         end) LOCAL_TYPE_NAME,
  2958.         convert(smallint,case
  2959.             when d.ss_dtype in (55,63,106,108) and t.xusertype > 255 then TypeProperty (t.name, 'Scale')
  2960.             else d.numeric_scale
  2961.         end) MINIMUM_SCALE,
  2962.         convert(smallint,case
  2963.             when d.ss_dtype in (55,63,106,108) and d.AUTO_INCREMENT = 0 and t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2964.             when d.ss_dtype in (55,63,106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2965.             else TypeProperty (t.name, 'Scale')
  2966.         end) MAXIMUM_SCALE,
  2967.         d.SQL_DATA_TYPE,
  2968.         d.SQL_DATETIME_SUB,
  2969.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2970.         INTERVAL_PRECISION = convert(smallint,NULL),
  2971.         USERTYPE = t.usertype
  2972.     from master.dbo.spt_datatype_info d
  2973.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2974.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2975.             t.xusertype = e.user_type
  2976.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2977.     where
  2978.         d.DATA_TYPE between @mintype and @maxtype
  2979.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2980.         and (t.xusertype <= 255 or
  2981.             isnull(d.AUTO_INCREMENT,0) = 0)
  2982.     order by 2, 12, 11,
  2983.     case
  2984.         when t.usertype=18 then 255
  2985.         else t.usertype
  2986.     end
  2987.  
  2988. go
  2989.  
  2990. grant execute on sp_datatype_info to public
  2991. go
  2992.  
  2993. dump tran master with no_log
  2994. go
  2995.  
  2996. print 'creating sp_fkeys'
  2997. go
  2998.  
  2999. /*  Procedure for pre-6.0 server */
  3000. CREATE PROCEDURE sp_fkeys(
  3001.                @pktable_name        varchar(32) = null,
  3002.                @pktable_owner       varchar(32) = null,
  3003.                @pktable_qualifier   varchar(32) = null,
  3004.                @fktable_name        varchar(32) = null,
  3005.                @fktable_owner       varchar(32) = null,
  3006.                @fktable_qualifier   varchar(32) = null )
  3007. as
  3008.     set nocount on
  3009.     declare @order_by_pk int
  3010.  
  3011.     select  @order_by_pk = 0
  3012.  
  3013.     if (@pktable_name is null) and (@fktable_name is null)
  3014.     begin   /* If neither primary key nor foreign key table names given */
  3015.         raiserror 20004 '~~Rush_25~~'
  3016.         return
  3017.     end
  3018.     if @fktable_qualifier is not null
  3019.     begin
  3020.         if db_name() <> @fktable_qualifier
  3021.         begin   /* If qualifier doesn't match current database */
  3022.             raiserror 20001 '~~Rush_26~~'
  3023.             return
  3024.         end
  3025.     end
  3026.     if @pktable_qualifier is not null
  3027.     begin
  3028.         if db_name() <> @pktable_qualifier
  3029.         begin   /* If qualifier doesn't match current database */
  3030.             raiserror 20001 '~~Rush_27~~'
  3031.             return
  3032.         end
  3033.     end
  3034.  
  3035.     if @pktable_name is null
  3036.     begin /*  If table name not supplied, match all */
  3037.         select @pktable_name = '%'
  3038.         select @order_by_pk = 1
  3039.     end
  3040.     if @pktable_owner is null   /*  If PK owner not supplied, match all */
  3041.         select @pktable_owner = '%'
  3042.     if @fktable_name is null    /*  If table name not supplied, match all */
  3043.         select @fktable_name = '%'
  3044.     if @fktable_owner is null   /*  If FK owner not supplied, match all */
  3045.         select @fktable_owner = '%'
  3046.  
  3047.     if @@trancount <> 0
  3048.     begin   /* If inside a transaction */
  3049.         raiserror 20003 '~~Rush_28~~'
  3050.         return
  3051.     end
  3052.     create table #fkeys(
  3053.              PKTABLE_QUALIFIER  varchar(32) NULL,
  3054.              PKTABLE_OWNER      varchar(32) NULL,
  3055.              PKTABLE_NAME       varchar(32) NOT NULL,
  3056.              PKCOLUMN_NAME      varchar(32) NOT NULL,
  3057.              FKTABLE_QUALIFIER  varchar(32) NULL,
  3058.              FKTABLE_OWNER      varchar(32) NULL,
  3059.              FKTABLE_NAME       varchar(32) NOT NULL,
  3060.              FKCOLUMN_NAME      varchar(32) NOT NULL,
  3061.              KEY_SEQ            smallint NOT NULL)
  3062.  
  3063.     /*  SQL Server supports upto 8 PK/FK relationships between 2 tables */
  3064.     /*  Process syskeys for each relationship */
  3065.     /*  The inserts below adds a row to the temp table for each of the
  3066.         8 possible relationships */
  3067.     insert into #fkeys
  3068.         select
  3069.             db_name(),
  3070.             user_name(o1.uid),
  3071.             object_name(k.depid),
  3072.             c2.name,
  3073.             db_name(),
  3074.             user_name(o2.uid),
  3075.             object_name(k.id),
  3076.             c1.name,
  3077.             1
  3078.         from
  3079.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3080.         where
  3081.             c1.id = k.id
  3082.             and k.type = 2  /* Foreign type key */
  3083.             and c1.colid = k.key1
  3084.             and c2.id = k.depid
  3085.             and c2.colid = k.depkey1
  3086.             and o1.id = k.depid
  3087.             and o2.id = k.id
  3088.     union all
  3089.         select
  3090.             db_name(),
  3091.             user_name(o1.uid),
  3092.             object_name(k.depid),
  3093.             c2.name,
  3094.             db_name(),
  3095.             user_name(o2.uid),
  3096.             object_name(k.id),
  3097.             c1.name,
  3098.             2
  3099.         from
  3100.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3101.         where
  3102.             c1.id = k.id
  3103.             and k.type = 2  /* Foreign type key */
  3104.             and c1.colid = k.key2
  3105.             and c2.id = k.depid
  3106.             and c2.colid = k.depkey2
  3107.             and o1.id = k.depid
  3108.             and o2.id = k.id
  3109.     union all
  3110.         select
  3111.             db_name(),
  3112.             user_name(o1.uid),
  3113.             object_name(k.depid),
  3114.             c2.name,
  3115.             db_name(),
  3116.             user_name(o2.uid),
  3117.             object_name(k.id),
  3118.             c1.name,
  3119.             3
  3120.         from
  3121.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3122.         where
  3123.             c1.id = k.id
  3124.             and k.type = 2  /* Foreign type key */
  3125.             and c1.colid = k.key3
  3126.             and c2.id = k.depid
  3127.             and c2.colid = k.depkey3
  3128.             and o1.id = k.depid
  3129.             and o2.id = k.id
  3130.     union all
  3131.         select
  3132.             db_name(),
  3133.             user_name(o1.uid),
  3134.             object_name(k.depid),
  3135.             c2.name,
  3136.             db_name(),
  3137.             user_name(o2.uid),
  3138.             object_name(k.id),
  3139.             c1.name,
  3140.             4
  3141.         from
  3142.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3143.         where
  3144.             c1.id = k.id
  3145.             and k.type = 2  /* Foreign type key */
  3146.             and c1.colid = k.key4
  3147.             and c2.id = k.depid
  3148.             and c2.colid = k.depkey4
  3149.             and o1.id = k.depid
  3150.             and o2.id = k.id
  3151.     union all
  3152.         select
  3153.             db_name(),
  3154.             user_name(o1.uid),
  3155.             object_name(k.depid),
  3156.             c2.name,
  3157.             db_name(),
  3158.             user_name(o2.uid),
  3159.             object_name(k.id),
  3160.             c1.name,
  3161.             5
  3162.         from
  3163.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3164.         where
  3165.             c1.id = k.id
  3166.             and k.type = 2  /* Foreign type key */
  3167.             and c1.colid = k.key5
  3168.             and c2.id = k.depid
  3169.             and c2.colid = k.depkey5
  3170.             and o1.id = k.depid
  3171.             and o2.id = k.id
  3172.     union all
  3173.         select
  3174.             db_name(),
  3175.             user_name(o1.uid),
  3176.             object_name(k.depid),
  3177.             c2.name,
  3178.             db_name(),
  3179.             user_name(o2.uid),
  3180.             object_name(k.id),
  3181.             c1.name,
  3182.             6
  3183.         from
  3184.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3185.         where
  3186.             c1.id = k.id
  3187.             and k.type = 2  /* Foreign type key */
  3188.             and c1.colid = k.key6
  3189.             and c2.id = k.depid
  3190.             and c2.colid = k.depkey6
  3191.             and o1.id = k.depid
  3192.             and o2.id = k.id
  3193.     union all
  3194.         select
  3195.             db_name(),
  3196.             user_name(o1.uid),
  3197.             object_name(k.depid),
  3198.             c2.name,
  3199.             db_name(),
  3200.             user_name(o2.uid),
  3201.             object_name(k.id),
  3202.             c1.name,
  3203.             7
  3204.         from
  3205.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3206.         where
  3207.             c1.id = k.id
  3208.             and k.type = 2  /* Foreign type key */
  3209.             and c1.colid = k.key7
  3210.             and c2.id = k.depid
  3211.             and c2.colid = k.depkey7
  3212.             and o1.id = k.depid
  3213.             and o2.id = k.id
  3214.     union all
  3215.         select
  3216.             db_name(),
  3217.             user_name(o1.uid),
  3218.             object_name(k.depid),
  3219.             c2.name,
  3220.             db_name(),
  3221.             user_name(o2.uid),
  3222.             object_name(k.id),
  3223.             c1.name,
  3224.             8
  3225.         from
  3226.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3227.         where
  3228.             c1.id = k.id
  3229.             and k.type = 2  /* Foreign type key */
  3230.             and c1.colid = k.key8
  3231.             and c2.id = k.depid
  3232.             and c2.colid = k.depkey8
  3233.             and o1.id = k.depid
  3234.             and o2.id = k.id
  3235.  
  3236.     if @order_by_pk = 1 /*  If order by PK fields */
  3237.         select
  3238.             PKTABLE_QUALIFIER,
  3239.             PKTABLE_OWNER,
  3240.             PKTABLE_NAME,
  3241.             PKCOLUMN_NAME,
  3242.             FKTABLE_QUALIFIER,
  3243.             FKTABLE_OWNER,
  3244.             FKTABLE_NAME,
  3245.             FKCOLUMN_NAME,
  3246.             KEY_SEQ,
  3247.             UPDATE_RULE = convert(smallint, null),
  3248.             DELETE_RULE = convert(smallint,null),
  3249.             FK_NAME = convert(varchar(32),null),
  3250.             PK_NAME = convert(varchar(32),null),
  3251.             DEFERRABILITY = convert(smallint,7)
  3252.         from #fkeys
  3253.         where FKTABLE_NAME like @fktable_name
  3254.             and FKTABLE_OWNER like @fktable_owner
  3255.             and PKTABLE_NAME  like @pktable_name
  3256.             and PKTABLE_OWNER like @pktable_owner
  3257.         order by 1, 2, 3, 9, 4
  3258.     else        /*  Order by FK fields */
  3259.         select
  3260.             PKTABLE_QUALIFIER,
  3261.             PKTABLE_OWNER,
  3262.             PKTABLE_NAME,
  3263.             PKCOLUMN_NAME,
  3264.             FKTABLE_QUALIFIER,
  3265.             FKTABLE_OWNER,
  3266.             FKTABLE_NAME,
  3267.             FKCOLUMN_NAME,
  3268.             KEY_SEQ,
  3269.             UPDATE_RULE = convert(smallint,null),
  3270.             DELETE_RULE = convert(smallint,null),
  3271.             FK_NAME = convert(varchar(32),null),
  3272.             PK_NAME = convert(varchar(32),null),
  3273.             DEFERRABILITY = convert(smallint,7)
  3274.         from #fkeys
  3275.         where FKTABLE_NAME like @fktable_name
  3276.             and FKTABLE_OWNER like @fktable_owner
  3277.             and PKTABLE_NAME  like @pktable_name
  3278.             and PKTABLE_OWNER like @pktable_owner
  3279.         order by 5, 6, 7, 9, 8
  3280. go
  3281.  
  3282. if (charindex('6.00', @@version) = 0 and
  3283.     charindex('6.50', @@version) = 0 and
  3284.     charindex('7.00', @@version) = 0 and
  3285.     charindex('8.00', @@version) = 0)
  3286. begin
  3287.     print ''
  3288.     print ''
  3289.     print 'Warning:'
  3290.     print 'you are installing the stored procedures '
  3291.     print 'on a pre 6.0 SQL Server.'
  3292.     print 'Ignore the following errors.'
  3293. end
  3294. else
  3295.     drop proc sp_fkeys
  3296. go
  3297.  
  3298. /*  Procedure for 6.0 and 6.50 servers */
  3299. CREATE PROCEDURE sp_fkeys(
  3300.                @pktable_name        varchar(32) = null,
  3301.                @pktable_owner       varchar(32) = null,
  3302.                @pktable_qualifier   varchar(32) = null,
  3303.                @fktable_name        varchar(32) = null,
  3304.                @fktable_owner       varchar(32) = null,
  3305.                @fktable_qualifier   varchar(32) = null )
  3306. as
  3307.     set nocount on
  3308.     DECLARE @pktable_id         int
  3309.     DECLARE @pkfull_table_name  varchar(65) /* 2*32 +1 */
  3310.     DECLARE @fktable_id         int
  3311.     DECLARE @fkfull_table_name  varchar(65) /* 2*32 +1 */
  3312.     declare @order_by_pk        int
  3313.  
  3314.     select  @order_by_pk = 0
  3315.  
  3316.     if (@pktable_name is null) and (@fktable_name is null)
  3317.     begin   /* If neither primary key nor foreign key table names given */
  3318.         raiserror (15252,-1,-1)
  3319.         return
  3320.     end
  3321.     if @fktable_qualifier is not null
  3322.     begin
  3323.         if db_name() <> @fktable_qualifier
  3324.         begin   /* If qualifier doesn't match current database */
  3325.             raiserror (15250, -1,-1)
  3326.             return
  3327.         end
  3328.     end
  3329.     if @pktable_qualifier is not null
  3330.     begin
  3331.         if db_name() <> @pktable_qualifier
  3332.         begin   /* If qualifier doesn't match current database */
  3333.             raiserror (15250, -1,-1)
  3334.             return
  3335.         end
  3336.     end
  3337.  
  3338.     if @pktable_owner is null
  3339.     begin   /* If unqualified primary key table name */
  3340.         SELECT @pkfull_table_name = @pktable_name
  3341.     end
  3342.     else
  3343.     begin   /* Qualified primary key table name */
  3344.         SELECT @pkfull_table_name = @pktable_owner + '.' + @pktable_name
  3345.     end
  3346.     /*  Get Object ID */
  3347.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3348.  
  3349.     if @fktable_owner is null
  3350.     begin   /* If unqualified foreign key table name */
  3351.         SELECT @fkfull_table_name = @fktable_name
  3352.     end
  3353.     else
  3354.     begin   /* Qualified foreign key table name */
  3355.         SELECT @fkfull_table_name = @fktable_owner + '.' + @fktable_name
  3356.     end
  3357.     /*  Get Object ID */
  3358.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3359.  
  3360.     if @fktable_name is not null
  3361.     begin
  3362.         if @fktable_id is null
  3363.             SELECT @fktable_id = 0  /* fk table not found, empty result */
  3364.     end
  3365.  
  3366.     if @pktable_name is null
  3367.     begin /*  If table name not supplied, match all */
  3368.         select @order_by_pk = 1
  3369.     end
  3370.     else
  3371.     begin
  3372.         if @pktable_id is null
  3373.         begin
  3374.             SELECT @pktable_id = 0  /* pk table not found, empty result */
  3375.         end
  3376.     end
  3377.  
  3378.     if (@@trancount <> 0 and
  3379.         charindex('6.50', @@version) = 0)
  3380.     begin   /* If inside a transaction */
  3381.         raiserror (15002,-1,-1,'sp_fkeys')
  3382.         return
  3383.     end
  3384.  
  3385.     create table #fkeys(
  3386.              pkdb_id        int NOT NULL,
  3387.              pktable_id     int NOT NULL,
  3388.              pkcolid        int NOT NULL,
  3389.              fkdb_id        int NOT NULL,
  3390.              fktable_id     int NOT NULL,
  3391.              fkcolid        int NOT NULL,
  3392.              KEY_SEQ        smallint NOT NULL,
  3393.              fk_id          int NOT NULL,
  3394.              pk_id          int NOT NULL)
  3395.  
  3396.     /*  SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3397.     /*  Process syskeys for each relationship */
  3398.     /*  The inserts below adds a row to the temp table for each of the
  3399.         16 possible relationships */
  3400.     insert into #fkeys
  3401.         select
  3402.             r.rkeydbid,
  3403.             r.rkeyid,
  3404.             r.rkey1,
  3405.             r.fkeydbid,
  3406.             r.fkeyid,
  3407.             r.fkey1,
  3408.             1,
  3409.             r.constid,
  3410.             s.constid
  3411.         from
  3412.             sysreferences r, sysconstraints s
  3413.         where   r.rkeyid = s.id
  3414.             AND (s.status & 0xf) = 1
  3415.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3416.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3417.       union all
  3418.         select
  3419.             r.rkeydbid,
  3420.             r.rkeyid,
  3421.             r.rkey2,
  3422.             r.fkeydbid,
  3423.             r.fkeyid,
  3424.             r.fkey2,
  3425.             2,
  3426.             r.constid,
  3427.             s.constid
  3428.         from
  3429.             sysreferences r, sysconstraints s
  3430.         where   r.rkeyid = s.id
  3431.             AND (s.status & 0xf) = 1
  3432.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3433.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3434.       union all
  3435.         select
  3436.             r.rkeydbid,
  3437.             r.rkeyid,
  3438.             r.rkey3,
  3439.             r.fkeydbid,
  3440.             r.fkeyid,
  3441.             r.fkey3,
  3442.             3,
  3443.             r.constid,
  3444.             s.constid
  3445.         from
  3446.             sysreferences r, sysconstraints s
  3447.         where   r.rkeyid = s.id
  3448.             AND (s.status & 0xf) = 1
  3449.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3450.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3451.       union all
  3452.         select
  3453.             r.rkeydbid,
  3454.             r.rkeyid,
  3455.             r.rkey4,
  3456.             r.fkeydbid,
  3457.             r.fkeyid,
  3458.             r.fkey4,
  3459.             4,
  3460.             r.constid,
  3461.             s.constid
  3462.         from
  3463.             sysreferences r, sysconstraints s
  3464.         where   r.rkeyid = s.id
  3465.             AND (s.status & 0xf) = 1
  3466.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3467.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3468.       union all
  3469.         select
  3470.             r.rkeydbid,
  3471.             r.rkeyid,
  3472.             r.rkey5,
  3473.             r.fkeydbid,
  3474.             r.fkeyid,
  3475.             r.fkey5,
  3476.             5,
  3477.             r.constid,
  3478.             s.constid
  3479.         from
  3480.             sysreferences r, sysconstraints s
  3481.         where   r.rkeyid = s.id
  3482.             AND (s.status & 0xf) = 1
  3483.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3484.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3485.       union all
  3486.         select
  3487.             r.rkeydbid,
  3488.             r.rkeyid,
  3489.             r.rkey6,
  3490.             r.fkeydbid,
  3491.             r.fkeyid,
  3492.             r.fkey6,
  3493.             6,
  3494.             r.constid,
  3495.             s.constid
  3496.         from
  3497.             sysreferences r, sysconstraints s
  3498.         where   r.rkeyid = s.id
  3499.             AND (s.status & 0xf) = 1
  3500.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3501.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3502.       union all
  3503.         select
  3504.             r.rkeydbid,
  3505.             r.rkeyid,
  3506.             r.rkey7,
  3507.             r.fkeydbid,
  3508.             r.fkeyid,
  3509.             r.fkey7,
  3510.             7,
  3511.             r.constid,
  3512.             s.constid
  3513.         from
  3514.             sysreferences r, sysconstraints s
  3515.         where   r.rkeyid = s.id
  3516.             AND (s.status & 0xf) = 1
  3517.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3518.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3519.       union all
  3520.         select
  3521.             r.rkeydbid,
  3522.             r.rkeyid,
  3523.             r.rkey8,
  3524.             r.fkeydbid,
  3525.             r.fkeyid,
  3526.             r.fkey8,
  3527.             8,
  3528.             r.constid,
  3529.             s.constid
  3530.         from
  3531.             sysreferences r, sysconstraints s
  3532.         where   r.rkeyid = s.id
  3533.             AND (s.status & 0xf) = 1
  3534.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3535.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3536.       union all
  3537.         select
  3538.             r.rkeydbid,
  3539.             r.rkeyid,
  3540.             r.rkey9,
  3541.             r.fkeydbid,
  3542.             r.fkeyid,
  3543.             r.fkey9,
  3544.             9,
  3545.             r.constid,
  3546.             s.constid
  3547.         from
  3548.             sysreferences r, sysconstraints s
  3549.         where   r.rkeyid = s.id
  3550.             AND (s.status & 0xf) = 1
  3551.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3552.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3553.       union all
  3554.         select
  3555.             r.rkeydbid,
  3556.             r.rkeyid,
  3557.             r.rkey10,
  3558.             r.fkeydbid,
  3559.             r.fkeyid,
  3560.             r.fkey10,
  3561.             10,
  3562.             r.constid,
  3563.             s.constid
  3564.         from
  3565.             sysreferences r, sysconstraints s
  3566.         where   r.rkeyid = s.id
  3567.             AND (s.status & 0xf) = 1
  3568.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3569.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3570.       union all
  3571.         select
  3572.             r.rkeydbid,
  3573.             r.rkeyid,
  3574.             r.rkey11,
  3575.             r.fkeydbid,
  3576.             r.fkeyid,
  3577.             r.fkey11,
  3578.             11,
  3579.             r.constid,
  3580.             s.constid
  3581.         from
  3582.             sysreferences r, sysconstraints s
  3583.         where   r.rkeyid = s.id
  3584.             AND (s.status & 0xf) = 1
  3585.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3586.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3587.       union all
  3588.         select
  3589.             r.rkeydbid,
  3590.             r.rkeyid,
  3591.             r.rkey12,
  3592.             r.fkeydbid,
  3593.             r.fkeyid,
  3594.             r.fkey12,
  3595.             12,
  3596.             r.constid,
  3597.             s.constid
  3598.         from
  3599.             sysreferences r, sysconstraints s
  3600.         where   r.rkeyid = s.id
  3601.             AND (s.status & 0xf) = 1
  3602.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3603.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3604.       union all
  3605.         select
  3606.             r.rkeydbid,
  3607.             r.rkeyid,
  3608.             r.rkey13,
  3609.             r.fkeydbid,
  3610.             r.fkeyid,
  3611.             r.fkey13,
  3612.             13,
  3613.             r.constid,
  3614.             s.constid
  3615.         from
  3616.             sysreferences r, sysconstraints s
  3617.         where   r.rkeyid = s.id
  3618.             AND (s.status & 0xf) = 1
  3619.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3620.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3621.       union all
  3622.         select
  3623.             r.rkeydbid,
  3624.             r.rkeyid,
  3625.             r.rkey14,
  3626.             r.fkeydbid,
  3627.             r.fkeyid,
  3628.             r.fkey14,
  3629.             14,
  3630.             r.constid,
  3631.             s.constid
  3632.         from
  3633.             sysreferences r, sysconstraints s
  3634.         where   r.rkeyid = s.id
  3635.             AND (s.status & 0xf) = 1
  3636.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3637.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3638.       union all
  3639.         select
  3640.             r.rkeydbid,
  3641.             r.rkeyid,
  3642.             r.rkey15,
  3643.             r.fkeydbid,
  3644.             r.fkeyid,
  3645.             r.fkey15,
  3646.             15,
  3647.             r.constid,
  3648.             s.constid
  3649.         from
  3650.             sysreferences r, sysconstraints s
  3651.         where   r.rkeyid = s.id
  3652.             AND (s.status & 0xf) = 1
  3653.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3654.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3655.       union all
  3656.         select
  3657.             r.rkeydbid,
  3658.             r.rkeyid,
  3659.             r.rkey16,
  3660.             r.fkeydbid,
  3661.             r.fkeyid,
  3662.             r.fkey16,
  3663.             16,
  3664.             r.constid,
  3665.             s.constid
  3666.         from
  3667.             sysreferences r, sysconstraints s
  3668.         where   r.rkeyid = s.id
  3669.             AND (s.status & 0xf) = 1
  3670.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3671.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3672.  
  3673.     if @order_by_pk = 1 /*  If order by PK fields */
  3674.         select
  3675.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3676.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3677.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3678.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3679.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3680.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3681.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3682.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3683.             KEY_SEQ,
  3684.             UPDATE_RULE = convert(smallint,1),
  3685.             DELETE_RULE = convert(smallint,1),
  3686.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3687.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3688.             DEFERRABILITY = convert(smallint,7)
  3689.         from #fkeys f,
  3690.             sysobjects o1, sysobjects o2,
  3691.             syscolumns c1, syscolumns c2
  3692.         where   o1.id = f.pktable_id
  3693.             AND o2.id = f.fktable_id
  3694.             AND c1.id = f.pktable_id
  3695.             AND c2.id = f.fktable_id
  3696.             AND c1.colid = f.pkcolid
  3697.             AND c2.colid = f.fkcolid
  3698.         order by 1,2,3,9,4
  3699.     else        /*  Order by FK fields */
  3700.         select
  3701.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3702.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3703.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3704.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3705.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3706.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3707.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3708.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3709.             KEY_SEQ,
  3710.             UPDATE_RULE = convert(smallint,1),
  3711.             DELETE_RULE = convert(smallint,1),
  3712.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3713.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3714.             DEFERRABILITY = convert(smallint,7)
  3715.         from #fkeys f,
  3716.             sysobjects o1, sysobjects o2,
  3717.             syscolumns c1, syscolumns c2
  3718.         where   o1.id = f.pktable_id
  3719.             AND o2.id = f.fktable_id
  3720.             AND c1.id = f.pktable_id
  3721.             AND c2.id = f.fktable_id
  3722.             AND c1.colid = f.pkcolid
  3723.             AND c2.colid = f.fkcolid
  3724.         order by 5,6,7,9,8
  3725. go
  3726.  
  3727. if (charindex('7.00', @@version) = 0 and
  3728.     charindex('8.00', @@version) = 0)
  3729. begin
  3730.     print ''
  3731.     print ''
  3732.     print 'Warning:'
  3733.     print 'you are installing the stored procedures '
  3734.     print 'on a pre 7.0 SQL Server.'
  3735.     print 'Ignore the following errors.'
  3736. end
  3737. else
  3738.     drop proc sp_fkeys
  3739. go
  3740.  
  3741. /*  Procedure for 7.0 server */
  3742. CREATE PROCEDURE sp_fkeys(
  3743.                @pktable_name        sysname = null,
  3744.                @pktable_owner       sysname = null,
  3745.                @pktable_qualifier   sysname = null,
  3746.                @fktable_name        sysname = null,
  3747.                @fktable_owner       sysname = null,
  3748.                @fktable_qualifier   sysname = null )
  3749. as
  3750.     set nocount on
  3751.     DECLARE @pktable_id         int
  3752.     DECLARE @pkfull_table_name  nvarchar(257) /* 2*128 + 1 */
  3753.     DECLARE @fktable_id         int
  3754.     DECLARE @fkfull_table_name  nvarchar(257) /* 2*128 + 1 */
  3755.     declare @order_by_pk        int
  3756.  
  3757.     /* select 'XXX starting table creation' */
  3758.  
  3759.     create table #fkeysall(
  3760.             rkeyid int NOT NULL,
  3761.             rkey1 int NOT NULL,
  3762.                 rkey2 int NOT NULL,
  3763.                 rkey3 int NOT NULL,
  3764.                 rkey4 int NOT NULL,
  3765.                 rkey5 int NOT NULL,
  3766.                 rkey6 int NOT NULL,
  3767.                 rkey7 int NOT NULL,
  3768.                 rkey8 int NOT NULL,
  3769.                 rkey9 int NOT NULL,
  3770.                 rkey10 int NOT NULL,
  3771.                 rkey11 int NOT NULL,
  3772.                 rkey12 int NOT NULL,
  3773.                 rkey13 int NOT NULL,
  3774.                 rkey14 int NOT NULL,
  3775.                 rkey15 int NOT NULL,
  3776.                 rkey16 int NOT NULL,
  3777.             fkeyid int NOT NULL,
  3778.             fkey1 int NOT NULL,
  3779.                 fkey2 int NOT NULL,
  3780.                 fkey3 int NOT NULL,
  3781.                 fkey4 int NOT NULL,
  3782.                 fkey5 int NOT NULL,
  3783.                 fkey6 int NOT NULL,
  3784.                 fkey7 int NOT NULL,
  3785.                 fkey8 int NOT NULL,
  3786.                 fkey9 int NOT NULL,
  3787.                 fkey10 int NOT NULL,
  3788.                 fkey11 int NOT NULL,
  3789.                 fkey12 int NOT NULL,
  3790.                 fkey13 int NOT NULL,
  3791.                 fkey14 int NOT NULL,
  3792.                 fkey15 int NOT NULL,
  3793.                 fkey16 int NOT NULL,
  3794.             constid int NOT NULL,
  3795.             name sysname NOT NULL)
  3796.  
  3797.     create table #fkeys(
  3798.             pktable_id      int NOT NULL,
  3799.             pkcolid         int NOT NULL,
  3800.             fktable_id      int NOT NULL,
  3801.             fkcolid         int NOT NULL,
  3802.             KEY_SEQ         smallint NOT NULL,
  3803.             fk_id           int NOT NULL,
  3804.             PK_NAME         sysname NOT NULL)
  3805.  
  3806.     create table #fkeysout(
  3807.             PKTABLE_QUALIFIER sysname NULL,
  3808.             PKTABLE_OWNER sysname NULL,
  3809.             PKTABLE_NAME sysname NOT NULL,
  3810.             PKCOLUMN_NAME sysname NOT NULL,
  3811.             FKTABLE_QUALIFIER sysname NULL,
  3812.             FKTABLE_OWNER sysname NULL,
  3813.             FKTABLE_NAME sysname NOT NULL,
  3814.             FKCOLUMN_NAME sysname NOT NULL,
  3815.             KEY_SEQ smallint NOT NULL,
  3816.             UPDATE_RULE smallint NULL,
  3817.             DELETE_RULE smallint NULL,
  3818.             FK_NAME sysname NULL,
  3819.             PK_NAME sysname NULL,
  3820.             DEFERRABILITY smallint null)
  3821.  
  3822.     /* select 'XXX starting parameter analysis' */
  3823.  
  3824.     select  @order_by_pk = 0
  3825.  
  3826.     if (@pktable_name is null) and (@fktable_name is null)
  3827.     begin   /* If neither primary key nor foreign key table names given */
  3828.         raiserror (15252,-1,-1)
  3829.         return
  3830.     end
  3831.     if @fktable_qualifier is not null
  3832.     begin
  3833.         if db_name() <> @fktable_qualifier
  3834.         begin   /* If qualifier doesn't match current database */
  3835.             raiserror (15250, -1,-1)
  3836.             return
  3837.         end
  3838.     end
  3839.     if @pktable_qualifier is not null
  3840.     begin
  3841.         if db_name() <> @pktable_qualifier
  3842.         begin   /* If qualifier doesn't match current database */
  3843.             raiserror (15250, -1,-1)
  3844.             return
  3845.         end
  3846.     end
  3847.  
  3848.     if @pktable_owner is null
  3849.     begin   /* If unqualified primary key table name */
  3850.         SELECT @pkfull_table_name = quotename(@pktable_name)
  3851.     end
  3852.     else
  3853.     begin   /* Qualified primary key table name */
  3854.         if @pktable_owner = ''
  3855.         begin   /* If empty owner name */
  3856.             SELECT @pkfull_table_name = quotename(@pktable_owner)
  3857.         end
  3858.         else
  3859.         begin
  3860.             SELECT @pkfull_table_name = quotename(@pktable_owner) +
  3861.                 '.' + quotename(@pktable_name)
  3862.         end
  3863.     end
  3864.     /*  Get Object ID */
  3865.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3866.  
  3867.     if @fktable_owner is null
  3868.     begin   /* If unqualified foreign key table name */
  3869.         SELECT @fkfull_table_name = quotename(@fktable_name)
  3870.     end
  3871.     else
  3872.     begin   /* Qualified foreign key table name */
  3873.         if @fktable_owner = ''
  3874.         begin   /* If empty owner name */
  3875.             SELECT @fkfull_table_name = quotename(@fktable_owner)
  3876.         end
  3877.         else
  3878.         begin
  3879.             SELECT @fkfull_table_name = quotename(@fktable_owner) +
  3880.                 '.' + quotename(@fktable_name)
  3881.         end
  3882.     end
  3883.     /*  Get Object ID */
  3884.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3885.  
  3886.     if @fktable_name is not null
  3887.     begin
  3888.         if @fktable_id is null
  3889.             SELECT @fktable_id = 0  /* fk table not found, empty result */
  3890.     end
  3891.  
  3892.     if @pktable_name is null
  3893.     begin /*  If table name not supplied, match all */
  3894.         select @order_by_pk = 1
  3895.     end
  3896.     else
  3897.     begin
  3898.         if @pktable_id is null
  3899.         begin
  3900.             SELECT @pktable_id = 0  /* pk table not found, empty result */
  3901.         end
  3902.     end
  3903.  
  3904.     /*  SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3905.     /*  Process syskeys for each relationship */
  3906.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  3907.         them out with a 16-way "insert select ... union select ..." */
  3908.  
  3909.     /* select 'XXX starting data analysis' */
  3910.  
  3911.     insert into #fkeysall
  3912.         select
  3913.             r.rkeyid,
  3914.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  3915.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  3916.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  3917.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  3918.             r.fkeyid,
  3919.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  3920.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  3921.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  3922.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  3923.             r.constid,
  3924.             i.name
  3925.         from
  3926.             sysreferences r, sysobjects o, sysindexes i
  3927.         where   r.constid = o.id
  3928.             AND o.xtype = 'F'
  3929.             AND r.rkeyindid = i.indid
  3930.             AND r.rkeyid = i.id
  3931.             AND r.rkeyid between isnull(@pktable_id, 0)
  3932.                             and isnull(@pktable_id, 0x7fffffff)
  3933.             AND r.fkeyid between isnull(@fktable_id, 0)
  3934.                             and isnull(@fktable_id, 0x7fffffff)
  3935.  
  3936.     /* select count (*) as 'XXX countall' from #fkeysall */
  3937.  
  3938.     insert into #fkeys
  3939.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  3940.             from #fkeysall
  3941.         union all
  3942.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  3943.             from #fkeysall
  3944.         union all
  3945.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  3946.             from #fkeysall
  3947.         union all
  3948.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  3949.             from #fkeysall
  3950.         union all
  3951.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  3952.             from #fkeysall
  3953.         union all
  3954.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  3955.             from #fkeysall
  3956.         union all
  3957.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  3958.             from #fkeysall
  3959.         union all
  3960.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  3961.             from #fkeysall
  3962.         union all
  3963.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  3964.             from #fkeysall
  3965.         union all
  3966.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  3967.             from #fkeysall
  3968.         union all
  3969.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  3970.             from #fkeysall
  3971.         union all
  3972.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  3973.             from #fkeysall
  3974.         union all
  3975.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  3976.             from #fkeysall
  3977.         union all
  3978.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  3979.             from #fkeysall
  3980.         union all
  3981.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  3982.             from #fkeysall
  3983.         union all
  3984.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  3985.             from #fkeysall
  3986.  
  3987.     /* select count (*) as 'XXX count' from #fkeys */
  3988.  
  3989.     insert into #fkeysout
  3990.         select
  3991.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  3992.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  3993.             PKTABLE_NAME = convert(sysname,o1.name),
  3994.             PKCOLUMN_NAME = convert(sysname,c1.name),
  3995.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  3996.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  3997.             FKTABLE_NAME = convert(sysname,o2.name),
  3998.             FKCOLUMN_NAME = convert(sysname,c2.name),
  3999.             KEY_SEQ,
  4000.             UPDATE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsUpdateCascade')=1) THEN 
  4001.                 convert(smallint,0) ELSE convert(smallint,1) END,
  4002.             DELETE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsDeleteCascade')=1) THEN 
  4003.                 convert(smallint,0) ELSE convert(smallint,1) END,
  4004.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  4005.             PK_NAME,
  4006.             DEFERRABILITY = 7   /* SQL_NOT_DEFERRABLE */
  4007.         from #fkeys f,
  4008.             sysobjects o1, sysobjects o2,
  4009.             syscolumns c1, syscolumns c2
  4010.         where   o1.id = f.pktable_id
  4011.             AND o2.id = f.fktable_id
  4012.             AND c1.id = f.pktable_id
  4013.             AND c2.id = f.fktable_id
  4014.             AND c1.colid = f.pkcolid
  4015.             AND c2.colid = f.fkcolid
  4016.     /* select count (*) as 'XXX countout' from #fkeysout */
  4017.  
  4018.     if @order_by_pk = 1 /*  If order by PK fields */
  4019.         select
  4020.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4021.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4022.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4023.         from #fkeysout
  4024.         order by 1,2,3,9,4
  4025.     else        /*  Order by FK fields */
  4026.         select
  4027.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4028.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4029.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4030.         from #fkeysout
  4031.         order by 5,6,7,9,8
  4032. go
  4033.  
  4034. if (charindex('8.00', @@version) = 0)
  4035. begin
  4036.     print ''
  4037.     print ''
  4038.     print 'Warning:'
  4039.     print 'you are installing the stored procedures '
  4040.     print 'on a pre 8.0 SQL Server.'
  4041.     print 'Ignore the following errors.'
  4042. end
  4043. else
  4044.     drop proc sp_fkeys
  4045. go
  4046.  
  4047. /*  Procedure for 8.0 server */
  4048. CREATE PROCEDURE sp_fkeys(
  4049.                @pktable_name        sysname = null,
  4050.                @pktable_owner       sysname = null,
  4051.                @pktable_qualifier   sysname = null,
  4052.                @fktable_name        sysname = null,
  4053.                @fktable_owner       sysname = null,
  4054.                @fktable_qualifier   sysname = null )
  4055. as
  4056.     set nocount on
  4057.     DECLARE @pktable_id         int
  4058.     DECLARE @pkfull_table_name  nvarchar(257) /* 2*128 + 1 */
  4059.     DECLARE @fktable_id         int
  4060.     DECLARE @fkfull_table_name  nvarchar(257) /* 2*128 + 1 */
  4061.     declare @order_by_pk        int
  4062.  
  4063.     /* select 'XXX starting table creation' */
  4064.  
  4065.     create table #fkeysall(
  4066.             rkeyid int NOT NULL,
  4067.             rkey1 int NOT NULL,
  4068.                 rkey2 int NOT NULL,
  4069.                 rkey3 int NOT NULL,
  4070.                 rkey4 int NOT NULL,
  4071.                 rkey5 int NOT NULL,
  4072.                 rkey6 int NOT NULL,
  4073.                 rkey7 int NOT NULL,
  4074.                 rkey8 int NOT NULL,
  4075.                 rkey9 int NOT NULL,
  4076.                 rkey10 int NOT NULL,
  4077.                 rkey11 int NOT NULL,
  4078.                 rkey12 int NOT NULL,
  4079.                 rkey13 int NOT NULL,
  4080.                 rkey14 int NOT NULL,
  4081.                 rkey15 int NOT NULL,
  4082.                 rkey16 int NOT NULL,
  4083.             fkeyid int NOT NULL,
  4084.             fkey1 int NOT NULL,
  4085.                 fkey2 int NOT NULL,
  4086.                 fkey3 int NOT NULL,
  4087.                 fkey4 int NOT NULL,
  4088.                 fkey5 int NOT NULL,
  4089.                 fkey6 int NOT NULL,
  4090.                 fkey7 int NOT NULL,
  4091.                 fkey8 int NOT NULL,
  4092.                 fkey9 int NOT NULL,
  4093.                 fkey10 int NOT NULL,
  4094.                 fkey11 int NOT NULL,
  4095.                 fkey12 int NOT NULL,
  4096.                 fkey13 int NOT NULL,
  4097.                 fkey14 int NOT NULL,
  4098.                 fkey15 int NOT NULL,
  4099.                 fkey16 int NOT NULL,
  4100.             constid int NOT NULL,
  4101.             name sysname collate database_default NOT NULL)
  4102.  
  4103.     create table #fkeys(
  4104.             pktable_id      int NOT NULL,
  4105.             pkcolid         int NOT NULL,
  4106.             fktable_id      int NOT NULL,
  4107.             fkcolid         int NOT NULL,
  4108.             KEY_SEQ         smallint NOT NULL,
  4109.             fk_id           int NOT NULL,
  4110.             PK_NAME         sysname collate database_default NOT NULL)
  4111.  
  4112.     create table #fkeysout(
  4113.             PKTABLE_QUALIFIER sysname collate database_default NULL,
  4114.             PKTABLE_OWNER sysname collate database_default NULL,
  4115.             PKTABLE_NAME sysname collate database_default NOT NULL,
  4116.             PKCOLUMN_NAME sysname collate database_default NOT NULL,
  4117.             FKTABLE_QUALIFIER sysname collate database_default NULL,
  4118.             FKTABLE_OWNER sysname collate database_default NULL,
  4119.             FKTABLE_NAME sysname collate database_default NOT NULL,
  4120.             FKCOLUMN_NAME sysname collate database_default NOT NULL,
  4121.             KEY_SEQ smallint NOT NULL,
  4122.             UPDATE_RULE smallint NULL,
  4123.             DELETE_RULE smallint NULL,
  4124.             FK_NAME sysname collate database_default NULL,
  4125.             PK_NAME sysname collate database_default NULL,
  4126.             DEFERRABILITY smallint null)
  4127.  
  4128.     /* select 'XXX starting parameter analysis' */
  4129.  
  4130.     select  @order_by_pk = 0
  4131.  
  4132.     if (@pktable_name is null) and (@fktable_name is null)
  4133.     begin   /* If neither primary key nor foreign key table names given */
  4134.         raiserror (15252,-1,-1)
  4135.         return
  4136.     end
  4137.     if @fktable_qualifier is not null
  4138.     begin
  4139.         if db_name() <> @fktable_qualifier
  4140.         begin   /* If qualifier doesn't match current database */
  4141.             raiserror (15250, -1,-1)
  4142.             return
  4143.         end
  4144.     end
  4145.     if @pktable_qualifier is not null
  4146.     begin
  4147.         if db_name() <> @pktable_qualifier
  4148.         begin   /* If qualifier doesn't match current database */
  4149.             raiserror (15250, -1,-1)
  4150.             return
  4151.         end
  4152.     end
  4153.  
  4154.     if @pktable_owner is null
  4155.     begin   /* If unqualified primary key table name */
  4156.         SELECT @pkfull_table_name = quotename(@pktable_name)
  4157.     end
  4158.     else
  4159.     begin   /* Qualified primary key table name */
  4160.         if @pktable_owner = ''
  4161.         begin   /* If empty owner name */
  4162.             SELECT @pkfull_table_name = quotename(@pktable_owner)
  4163.         end
  4164.         else
  4165.         begin
  4166.             SELECT @pkfull_table_name = quotename(@pktable_owner) +
  4167.                 '.' + quotename(@pktable_name)
  4168.         end
  4169.     end
  4170.     /*  Get Object ID */
  4171.     SELECT @pktable_id = object_id(@pkfull_table_name)
  4172.  
  4173.     if @fktable_owner is null
  4174.     begin   /* If unqualified foreign key table name */
  4175.         SELECT @fkfull_table_name = quotename(@fktable_name)
  4176.     end
  4177.     else
  4178.     begin   /* Qualified foreign key table name */
  4179.         if @fktable_owner = ''
  4180.         begin   /* If empty owner name */
  4181.             SELECT @fkfull_table_name = quotename(@fktable_owner)
  4182.         end
  4183.         else
  4184.         begin
  4185.             SELECT @fkfull_table_name = quotename(@fktable_owner) +
  4186.                 '.' + quotename(@fktable_name)
  4187.         end
  4188.     end
  4189.     /*  Get Object ID */
  4190.     SELECT @fktable_id = object_id(@fkfull_table_name)
  4191.  
  4192.     if @fktable_name is not null
  4193.     begin
  4194.         if @fktable_id is null
  4195.             SELECT @fktable_id = 0  /* fk table not found, empty result */
  4196.     end
  4197.  
  4198.     if @pktable_name is null
  4199.     begin /*  If table name not supplied, match all */
  4200.         select @order_by_pk = 1
  4201.     end
  4202.     else
  4203.     begin
  4204.         if @pktable_id is null
  4205.         begin
  4206.             SELECT @pktable_id = 0  /* pk table not found, empty result */
  4207.         end
  4208.     end
  4209.  
  4210.     /*  SQL Server supports upto 16 PK/FK relationships between 2 tables */
  4211.     /*  Process syskeys for each relationship */
  4212.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  4213.         them out with a 16-way "insert select ... union select ..." */
  4214.  
  4215.     /* select 'XXX starting data analysis' */
  4216.  
  4217.     insert into #fkeysall
  4218.         select
  4219.             r.rkeyid,
  4220.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  4221.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  4222.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  4223.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  4224.             r.fkeyid,
  4225.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  4226.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  4227.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  4228.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  4229.             r.constid,
  4230.             i.name
  4231.         from
  4232.             sysreferences r, sysobjects o, sysindexes i
  4233.         where   r.constid = o.id
  4234.             AND o.xtype = 'F'
  4235.             AND r.rkeyindid = i.indid
  4236.             AND r.rkeyid = i.id
  4237.             AND r.rkeyid between isnull(@pktable_id, 0)
  4238.                             and isnull(@pktable_id, 0x7fffffff)
  4239.             AND r.fkeyid between isnull(@fktable_id, 0)
  4240.                             and isnull(@fktable_id, 0x7fffffff)
  4241.  
  4242.     /* select count (*) as 'XXX countall' from #fkeysall */
  4243.  
  4244.     insert into #fkeys
  4245.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  4246.             from #fkeysall
  4247.         union all
  4248.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  4249.             from #fkeysall
  4250.         union all
  4251.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  4252.             from #fkeysall
  4253.         union all
  4254.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  4255.             from #fkeysall
  4256.         union all
  4257.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  4258.             from #fkeysall
  4259.         union all
  4260.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  4261.             from #fkeysall
  4262.         union all
  4263.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  4264.             from #fkeysall
  4265.         union all
  4266.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  4267.             from #fkeysall
  4268.         union all
  4269.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  4270.             from #fkeysall
  4271.         union all
  4272.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  4273.             from #fkeysall
  4274.         union all
  4275.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  4276.             from #fkeysall
  4277.         union all
  4278.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  4279.             from #fkeysall
  4280.         union all
  4281.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  4282.             from #fkeysall
  4283.         union all
  4284.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  4285.             from #fkeysall
  4286.         union all
  4287.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  4288.             from #fkeysall
  4289.         union all
  4290.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  4291.             from #fkeysall
  4292.  
  4293.     /* select count (*) as 'XXX count' from #fkeys */
  4294.  
  4295.     insert into #fkeysout
  4296.         select
  4297.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  4298.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  4299.             PKTABLE_NAME = convert(sysname,o1.name),
  4300.             PKCOLUMN_NAME = convert(sysname,c1.name),
  4301.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  4302.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  4303.             FKTABLE_NAME = convert(sysname,o2.name),
  4304.             FKCOLUMN_NAME = convert(sysname,c2.name),
  4305.             KEY_SEQ,
  4306.             UPDATE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsUpdateCascade')=1) THEN 
  4307.                 convert(smallint,0) ELSE convert(smallint,1) END,
  4308.             DELETE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsDeleteCascade')=1) THEN 
  4309.                 convert(smallint,0) ELSE convert(smallint,1) END,
  4310.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  4311.             PK_NAME,
  4312.             DEFERRABILITY = 7   /* SQL_NOT_DEFERRABLE */
  4313.         from #fkeys f,
  4314.             sysobjects o1, sysobjects o2,
  4315.             syscolumns c1, syscolumns c2
  4316.         where   o1.id = f.pktable_id
  4317.             AND o2.id = f.fktable_id
  4318.             AND c1.id = f.pktable_id
  4319.             AND c2.id = f.fktable_id
  4320.             AND c1.colid = f.pkcolid
  4321.             AND c2.colid = f.fkcolid
  4322.     /* select count (*) as 'XXX countout' from #fkeysout */
  4323.  
  4324.     if @order_by_pk = 1 /*  If order by PK fields */
  4325.         select
  4326.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4327.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4328.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4329.         from #fkeysout
  4330.         order by 1,2,3,9,4
  4331.     else        /*  Order by FK fields */
  4332.         select
  4333.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4334.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4335.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4336.         from #fkeysout
  4337.         order by 5,6,7,9,8
  4338. go
  4339. grant execute on sp_fkeys to public
  4340. go
  4341.  
  4342. dump tran master with no_log
  4343. go
  4344.  
  4345. print 'creating sp_pkeys'
  4346. go
  4347.  
  4348. /*  Procedure for pre-6.0 server */
  4349. CREATE PROCEDURE sp_pkeys(
  4350.                @table_name      varchar(32),
  4351.                @table_owner     varchar(32) = null,
  4352.                @table_qualifier varchar(32) = null )
  4353. as
  4354.     set nocount on
  4355.     if @table_qualifier is not null
  4356.     begin
  4357.         if db_name() <> @table_qualifier
  4358.         begin   /* If qualifier doesn't match current database */
  4359.             raiserror 20001 '~~Rush_5~~'
  4360.             return
  4361.         end
  4362.     end
  4363.     if @table_owner is null /*  If owner not supplied, match all */
  4364.         select @table_owner = '%'
  4365.     if @@trancount <> 0
  4366.     begin   /* If inside a transaction */
  4367.         raiserror 20003 '~~Rush_35~~'
  4368.         return
  4369.     end
  4370.  
  4371.     create table #pkeys(
  4372.              TABLE_QUALIFIER varchar(32) NULL,
  4373.              TABLE_OWNER     varchar(32) NULL,
  4374.              TABLE_NAME      varchar(32) NOT NULL,
  4375.              COLUMN_NAME     varchar(32) NOT NULL,
  4376.              KEY_SEQ         smallint NOT NULL)
  4377.  
  4378.     /*  SQL Server supports upto 8 PK/FK relationships between 2 tables */
  4379.     /*  Process syskeys for each relationship */
  4380.     /*  The inserts below adds a row to the temp table for each of the
  4381.         8 possible relationships */
  4382.     insert into #pkeys
  4383.         select
  4384.             db_name(),
  4385.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4386.             object_name(k.id),
  4387.             c.name,
  4388.             1
  4389.         from
  4390.             syskeys k, syscolumns c
  4391.         where
  4392.             c.id = k.id
  4393.             and k.type = 1  /* Primary type key */
  4394.             and c.colid = k.key1
  4395.     if (@@rowcount = 0)
  4396.         goto done
  4397.  
  4398.     insert into #pkeys
  4399.         select
  4400.             db_name(),
  4401.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4402.             object_name(k.id),
  4403.             c.name,
  4404.             2
  4405.         from
  4406.             syskeys k, syscolumns c
  4407.         where
  4408.             c.id = k.id
  4409.             and k.type = 1  /* Primary type key */
  4410.             and c.colid = key2
  4411.     if (@@rowcount = 0)
  4412.         goto done
  4413.  
  4414.     insert into #pkeys
  4415.         select
  4416.             db_name(),
  4417.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4418.             object_name(k.id),
  4419.             c.name,
  4420.             3
  4421.         from
  4422.             syskeys k, syscolumns c
  4423.         where
  4424.             c.id = k.id
  4425.             and k.type = 1  /* Primary type key */
  4426.             and c.colid = key3
  4427.     if (@@rowcount = 0)
  4428.         goto done
  4429.  
  4430.     insert into #pkeys
  4431.         select
  4432.             db_name(),
  4433.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4434.             object_name(k.id),
  4435.             c.name,
  4436.             4
  4437.         from
  4438.             syskeys k, syscolumns c
  4439.         where
  4440.             c.id = k.id
  4441.             and k.type = 1  /* Primary type key */
  4442.             and c.colid = key4
  4443.     if (@@rowcount = 0)
  4444.         goto done
  4445.  
  4446.     insert into #pkeys
  4447.         select
  4448.             db_name(),
  4449.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4450.             object_name(k.id),
  4451.             c.name,
  4452.             5
  4453.         from
  4454.             syskeys k, syscolumns c
  4455.         where
  4456.             c.id = k.id
  4457.             and k.type = 1  /* Primary type key */
  4458.             and c.colid = key5
  4459.     if (@@rowcount = 0)
  4460.         goto done
  4461.  
  4462.     insert into #pkeys
  4463.         select
  4464.             db_name(),
  4465.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4466.             object_name(k.id),
  4467.             c.name,
  4468.             6
  4469.         from
  4470.             syskeys k, syscolumns c
  4471.         where
  4472.             c.id = k.id
  4473.             and k.type = 1  /* Primary type key */
  4474.             and c.colid = key6
  4475.     if (@@rowcount = 0)
  4476.         goto done
  4477.  
  4478.     insert into #pkeys
  4479.         select
  4480.             db_name(),
  4481.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4482.             object_name(k.id),
  4483.             c.name,
  4484.             7
  4485.         from
  4486.             syskeys k, syscolumns c
  4487.         where
  4488.             c.id = k.id
  4489.             and k.type = 1  /* Primary type key */
  4490.             and c.colid = key7
  4491.     if (@@rowcount = 0)
  4492.         goto done
  4493.  
  4494.     insert into #pkeys
  4495.          select
  4496.              db_name(),
  4497.              (select user_name(uid) from sysobjects o where o.id = k.id),
  4498.              object_name(k.id),
  4499.              c.name,
  4500.              8
  4501.          from
  4502.              syskeys k, syscolumns c
  4503.          where
  4504.              c.id = k.id
  4505.              and k.type = 1 /* Primary type key */
  4506.              and c.colid = key8
  4507.  
  4508.     done:
  4509.     select
  4510.         TABLE_QUALIFIER,
  4511.         TABLE_OWNER,
  4512.         TABLE_NAME,
  4513.         COLUMN_NAME,
  4514.         KEY_SEQ,
  4515.         PK_NAME = convert(varchar(32),null)
  4516.     from #pkeys
  4517.     where TABLE_NAME = @table_name
  4518.         and TABLE_OWNER like @table_owner
  4519.     order by 1, 2, 3, 5
  4520. go
  4521.  
  4522. if (charindex('6.00', @@version) = 0 and
  4523.     charindex('6.50', @@version) = 0 and
  4524.     charindex('7.00', @@version) = 0 and
  4525.     charindex('8.00', @@version) = 0)
  4526. begin
  4527.     print ''
  4528.     print ''
  4529.     print 'Warning:'
  4530.     print 'you are installing the stored procedures '
  4531.     print 'on a pre 6.0 SQL Server.'
  4532.     print 'Ignore the following error.'
  4533. end
  4534. else
  4535.     drop proc sp_pkeys
  4536. go
  4537.  
  4538. /*  Procedure for 6.0 and 6.50 servers */
  4539. CREATE PROCEDURE sp_pkeys(
  4540.                @table_name      sysname,
  4541.                @table_owner     sysname = null,
  4542.                @table_qualifier sysname = null )
  4543. as
  4544.     DECLARE @table_id           int
  4545.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  4546.  
  4547.     if @table_qualifier is not null
  4548.     begin
  4549.         if db_name() <> @table_qualifier
  4550.         begin   /* If qualifier doesn't match current database */
  4551.             raiserror (15250, -1,-1)
  4552.             return
  4553.         end
  4554.     end
  4555.     if @table_owner is null
  4556.     begin   /* If unqualified table name */
  4557.         SELECT @full_table_name = @table_name
  4558.     end
  4559.     else
  4560.     begin   /* Qualified table name */
  4561.         if @table_owner = ''
  4562.         begin   /* If empty owner name */
  4563.             SELECT @full_table_name = @table_owner
  4564.         end
  4565.         else
  4566.         begin
  4567.             SELECT @full_table_name = @table_owner + '.' + @table_name
  4568.         end
  4569.     end
  4570.     /*  Get Object ID */
  4571.     SELECT @table_id = object_id(@full_table_name)
  4572.  
  4573.     select
  4574.         TABLE_QUALIFIER = convert(sysname,db_name()),
  4575.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  4576.         TABLE_NAME = convert(sysname,o.name),
  4577.         COLUMN_NAME = convert(sysname,c.name),
  4578.         KEY_SEQ =
  4579.             case
  4580.                 when c.name = index_col(@full_table_name, i.indid,  1) then convert (smallint,1)
  4581.                 when c.name = index_col(@full_table_name, i.indid,  2) then convert (smallint,2)
  4582.                 when c.name = index_col(@full_table_name, i.indid,  3) then convert (smallint,3)
  4583.                 when c.name = index_col(@full_table_name, i.indid,  4) then convert (smallint,4)
  4584.                 when c.name = index_col(@full_table_name, i.indid,  5) then convert (smallint,5)
  4585.                 when c.name = index_col(@full_table_name, i.indid,  6) then convert (smallint,6)
  4586.                 when c.name = index_col(@full_table_name, i.indid,  7) then convert (smallint,7)
  4587.                 when c.name = index_col(@full_table_name, i.indid,  8) then convert (smallint,8)
  4588.                 when c.name = index_col(@full_table_name, i.indid,  9) then convert (smallint,9)
  4589.                 when c.name = index_col(@full_table_name, i.indid, 10) then convert (smallint,10)
  4590.                 when c.name = index_col(@full_table_name, i.indid, 11) then convert (smallint,11)
  4591.                 when c.name = index_col(@full_table_name, i.indid, 12) then convert (smallint,12)
  4592.                 when c.name = index_col(@full_table_name, i.indid, 13) then convert (smallint,13)
  4593.                 when c.name = index_col(@full_table_name, i.indid, 14) then convert (smallint,14)
  4594.                 when c.name = index_col(@full_table_name, i.indid, 15) then convert (smallint,15)
  4595.                 when c.name = index_col(@full_table_name, i.indid, 16) then convert (smallint,16)
  4596.             end,
  4597.         PK_NAME = convert(sysname,i.name)
  4598.     from
  4599.         sysindexes i, syscolumns c, sysobjects o
  4600.     where
  4601.         o.id = @table_id
  4602.         and o.id = c.id
  4603.         and o.id = i.id
  4604.         and (i.status & 0x800) = 0x800
  4605.         and (c.name = index_col (@full_table_name, i.indid,  1) or
  4606.              c.name = index_col (@full_table_name, i.indid,  2) or
  4607.              c.name = index_col (@full_table_name, i.indid,  3) or
  4608.              c.name = index_col (@full_table_name, i.indid,  4) or
  4609.              c.name = index_col (@full_table_name, i.indid,  5) or
  4610.              c.name = index_col (@full_table_name, i.indid,  6) or
  4611.              c.name = index_col (@full_table_name, i.indid,  7) or
  4612.              c.name = index_col (@full_table_name, i.indid,  8) or
  4613.              c.name = index_col (@full_table_name, i.indid,  9) or
  4614.              c.name = index_col (@full_table_name, i.indid, 10) or
  4615.              c.name = index_col (@full_table_name, i.indid, 11) or
  4616.              c.name = index_col (@full_table_name, i.indid, 12) or
  4617.              c.name = index_col (@full_table_name, i.indid, 13) or
  4618.              c.name = index_col (@full_table_name, i.indid, 14) or
  4619.              c.name = index_col (@full_table_name, i.indid, 15) or
  4620.              c.name = index_col (@full_table_name, i.indid, 16)
  4621.             )
  4622.     order by 1, 2, 3, 5
  4623. go
  4624.  
  4625. if (charindex('7.00', @@version) = 0 and
  4626.     charindex('8.00', @@version) = 0)
  4627. begin
  4628.     print ''
  4629.     print ''
  4630.     print 'Warning:'
  4631.     print 'you are installing the stored procedures '
  4632.     print 'on a pre 7.0 SQL Server.'
  4633.     print 'Ignore the following errors.'
  4634. end
  4635. else
  4636.     drop proc sp_pkeys
  4637. go
  4638.  
  4639. /*  Procedure for 7.0 & 8.0 servers */
  4640. CREATE PROCEDURE sp_pkeys(
  4641.                @table_name      sysname,
  4642.                @table_owner     sysname = null,
  4643.                @table_qualifier sysname = null )
  4644. as
  4645.     DECLARE @table_id       int
  4646.     DECLARE @full_table_name    nvarchar(255)
  4647.  
  4648.     if @table_qualifier is not null
  4649.     begin
  4650.         if db_name() <> @table_qualifier
  4651.         begin   /* If qualifier doesn't match current database */
  4652.             raiserror (15250, -1,-1)
  4653.             return
  4654.         end
  4655.     end
  4656.     if @table_owner is null
  4657.     begin   /* If unqualified table name */
  4658.         SELECT @full_table_name = quotename(@table_name)
  4659.     end
  4660.     else
  4661.     begin   /* Qualified table name */
  4662.         if @table_owner = ''
  4663.         begin   /* If empty owner name */
  4664.             SELECT @full_table_name = quotename(@table_owner)
  4665.         end
  4666.         else
  4667.         begin
  4668.             SELECT @full_table_name = quotename(@table_owner) +
  4669.                 '.' + quotename(@table_name)
  4670.         end
  4671.     end
  4672.     /*  Get Object ID */
  4673.     SELECT @table_id = object_id(@full_table_name)
  4674.  
  4675.     select
  4676.         TABLE_QUALIFIER = convert(sysname,db_name()),
  4677.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  4678.         TABLE_NAME = convert(sysname,o.name),
  4679.         COLUMN_NAME = convert(sysname,c.name),
  4680.         KEY_SEQ =
  4681.             case
  4682.                 when c.name = index_col(@full_table_name, i.indid,  1) then convert (smallint,1)
  4683.                 when c.name = index_col(@full_table_name, i.indid,  2) then convert (smallint,2)
  4684.                 when c.name = index_col(@full_table_name, i.indid,  3) then convert (smallint,3)
  4685.                 when c.name = index_col(@full_table_name, i.indid,  4) then convert (smallint,4)
  4686.                 when c.name = index_col(@full_table_name, i.indid,  5) then convert (smallint,5)
  4687.                 when c.name = index_col(@full_table_name, i.indid,  6) then convert (smallint,6)
  4688.                 when c.name = index_col(@full_table_name, i.indid,  7) then convert (smallint,7)
  4689.                 when c.name = index_col(@full_table_name, i.indid,  8) then convert (smallint,8)
  4690.                 when c.name = index_col(@full_table_name, i.indid,  9) then convert (smallint,9)
  4691.                 when c.name = index_col(@full_table_name, i.indid, 10) then convert (smallint,10)
  4692.                 when c.name = index_col(@full_table_name, i.indid, 11) then convert (smallint,11)
  4693.                 when c.name = index_col(@full_table_name, i.indid, 12) then convert (smallint,12)
  4694.                 when c.name = index_col(@full_table_name, i.indid, 13) then convert (smallint,13)
  4695.                 when c.name = index_col(@full_table_name, i.indid, 14) then convert (smallint,14)
  4696.                 when c.name = index_col(@full_table_name, i.indid, 15) then convert (smallint,15)
  4697.                 when c.name = index_col(@full_table_name, i.indid, 16) then convert (smallint,16)
  4698.             end,
  4699.         PK_NAME = convert(sysname,i.name)
  4700.     from
  4701.         sysindexes i, syscolumns c, sysobjects o --, syscolumns c1
  4702.     where
  4703.         o.id = @table_id
  4704.         and o.id = c.id
  4705.         and o.id = i.id
  4706.         and (i.status & 0x800) = 0x800
  4707.         and (c.name = index_col (@full_table_name, i.indid,  1) or
  4708.              c.name = index_col (@full_table_name, i.indid,  2) or
  4709.              c.name = index_col (@full_table_name, i.indid,  3) or
  4710.              c.name = index_col (@full_table_name, i.indid,  4) or
  4711.              c.name = index_col (@full_table_name, i.indid,  5) or
  4712.              c.name = index_col (@full_table_name, i.indid,  6) or
  4713.              c.name = index_col (@full_table_name, i.indid,  7) or
  4714.              c.name = index_col (@full_table_name, i.indid,  8) or
  4715.              c.name = index_col (@full_table_name, i.indid,  9) or
  4716.              c.name = index_col (@full_table_name, i.indid, 10) or
  4717.              c.name = index_col (@full_table_name, i.indid, 11) or
  4718.              c.name = index_col (@full_table_name, i.indid, 12) or
  4719.              c.name = index_col (@full_table_name, i.indid, 13) or
  4720.              c.name = index_col (@full_table_name, i.indid, 14) or
  4721.              c.name = index_col (@full_table_name, i.indid, 15) or
  4722.              c.name = index_col (@full_table_name, i.indid, 16)
  4723.             )
  4724.     order by 1, 2, 3, 5
  4725. go
  4726.  
  4727.  
  4728. grant execute on sp_pkeys to public
  4729. go
  4730.  
  4731. dump tran master with no_log
  4732. go
  4733.  
  4734. print 'creating sp_server_info'
  4735. go
  4736.  
  4737. create proc sp_server_info (
  4738.             @attribute_id  int = null)
  4739. as
  4740.     if @attribute_id is not null
  4741.         select *
  4742.         from master.dbo.spt_server_info
  4743.         where attribute_id = @attribute_id
  4744.     else
  4745.         select *
  4746.         from master.dbo.spt_server_info
  4747.         order by attribute_id
  4748. go
  4749.  
  4750. grant execute on sp_server_info to public
  4751. go
  4752.  
  4753. dump tran master with no_log
  4754. go
  4755.  
  4756. print 'creating sp_special_columns'
  4757. go
  4758.  
  4759. /*  Procedure for pre-6.0 server */
  4760. CREATE PROCEDURE sp_special_columns (
  4761.                  @table_name        varchar(32),
  4762.                  @table_owner       varchar(32) = null,
  4763.                  @table_qualifier   varchar(32) = null,
  4764.                  @col_type          char(1) = 'R',
  4765.                  @scope             char(1) = 'T',
  4766.                  @nullable          char(1) = 'U',
  4767.                  @ODBCVer           int = 2)
  4768. AS
  4769.     DECLARE @indid              int
  4770.     DECLARE @table_id           int
  4771.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4772.     DECLARE @scopeout           smallint
  4773.  
  4774.     if @col_type not in ('R','V') or @col_type is null
  4775.     begin
  4776.         raiserror 20002 '~~Rush_42~~'
  4777.         return
  4778.     end
  4779.  
  4780.     if @scope = 'C'
  4781.         select @scopeout = 0
  4782.     else if @scope = 'T'
  4783.         select @scopeout = 1
  4784.     else
  4785.     begin
  4786.         raiserror 20002 '~~Rush_43~~'
  4787.         return
  4788.     end
  4789.  
  4790.     if @nullable not in ('U','O') or @nullable is null
  4791.     begin
  4792.         raiserror 20002 '~~Rush_44~~'
  4793.         return
  4794.     end
  4795.  
  4796.     if @table_qualifier is not null
  4797.     begin
  4798.         if db_name() <> @table_qualifier
  4799.         begin /* If qualifier doesn't match current database */
  4800.             raiserror 20001 '~~Rush_5~~'
  4801.             return
  4802.         end
  4803.     end
  4804.     if @table_owner is null
  4805.     begin    /* If unqualified table name */
  4806.         SELECT @full_table_name = @table_name
  4807.     end
  4808.     else
  4809.     begin   /* Qualified table name */
  4810.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4811.     end
  4812.     /*  Get Object ID */
  4813.     SELECT @table_id = object_id(@full_table_name)
  4814.  
  4815.     if @col_type = 'V'
  4816.     BEGIN /* if ROWVER, just run that query */
  4817.         SELECT
  4818.             SCOPE = convert(smallint,NULL),
  4819.             COLUMN_NAME = convert(varchar(32),c.name),
  4820.             DATA_TYPE = convert(smallint, -3),
  4821.             TYPE_NAME = t.name,
  4822.             "PRECISION" = convert(int,8),
  4823.             LENGTH = convert(int,8),
  4824.             SCALE = convert(smallint, NULL),
  4825.             PSEUDO_COLUMN = convert(smallint,1)
  4826.         FROM
  4827.             systypes t, syscolumns c
  4828.         WHERE
  4829.             c.id = @table_id
  4830.             AND c.usertype = 80 /*  TIMESTAMP */
  4831.             AND t.usertype = 80 /*  TIMESTAMP */
  4832.         RETURN
  4833.     END
  4834.  
  4835.     /* ROWID, now find the id of the 'best' index for this table */
  4836.  
  4837.     IF @nullable = 'O'  /* Don't include any indexes that contain
  4838.                            nullable columns. */
  4839.  
  4840.             SELECT @indid = MIN(indid)
  4841.                 FROM sysindexes i,syscolumns c,syscolumns c2
  4842.                 WHERE
  4843.                     i.status&2 = 2      /*  If Unique Index */
  4844.                     AND c.id = i.id
  4845.                     AND c2.id = c.id
  4846.                     AND c2.colid < i.keycnt + (i.status&16)/16
  4847.                     AND i.id = @table_id
  4848.                     AND indid > 0       /*  Eliminate Table Row */
  4849.                     AND c.name = index_col(@table_name,i.indid,c2.colid)
  4850.                     GROUP BY indid HAVING SUM(c.status&8) = 0
  4851.  
  4852.     ELSE    /* Include indexes that are partially nullable. */
  4853.  
  4854.         SELECT @indid = MIN(indid)
  4855.             FROM sysindexes i
  4856.             WHERE
  4857.                 status&2 = 2        /*  If Unique Index */
  4858.                 AND id = @table_id
  4859.                 AND indid > 0       /*  Eliminate Table Row */
  4860.  
  4861.     SELECT
  4862.         SCOPE = @scopeout,
  4863.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  4864.         d.DATA_TYPE,
  4865.         TYPE_NAME = t.name,
  4866.         "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4867.         LENGTH = isnull(d.length, convert(int,c.length)),
  4868.         SCALE = d.numeric_scale,
  4869.         PSEUDO_COLUMN = convert(smallint,1)
  4870.     FROM
  4871.         sysindexes x,
  4872.         syscolumns c,
  4873.         master.dbo.spt_datatype_info d,
  4874.         systypes t,
  4875.         syscolumns c2   /* Self-join to generate list of index columns and */
  4876.                         /* to extract datatype names */
  4877.     WHERE
  4878.         x.id = @table_id
  4879.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4880.         AND c.id = x.id
  4881.         AND c2.id = x.id
  4882.         AND c2.colid < keycnt+(x.status&16)/16
  4883.         AND x.indid = @indid
  4884.         AND t.type = d.ss_dtype
  4885.         AND c.length = d.fixlen
  4886.         AND c.usertype = t.usertype
  4887.  
  4888. go
  4889.  
  4890. if (charindex('6.00', @@version) = 0 and
  4891.     charindex('6.50', @@version) = 0 and
  4892.     charindex('7.00', @@version) = 0 and
  4893.     charindex('8.00', @@version) = 0)
  4894. begin
  4895.     print ''
  4896.     print ''
  4897.     print 'Warning:'
  4898.     print 'you are installing the stored procedures '
  4899.     print 'on a pre 6.0 SQL Server.'
  4900.     print 'Ignore the following errors.'
  4901. end
  4902. else
  4903.     drop proc sp_special_columns
  4904. go
  4905.  
  4906. /*  Procedure for 6.0 and 6.50 servers */
  4907. CREATE PROCEDURE sp_special_columns (
  4908.                  @table_name        varchar(32),
  4909.                  @table_owner       varchar(32) = null,
  4910.                  @table_qualifier   varchar(32) = null,
  4911.                  @col_type          char(1) = 'R',
  4912.                  @scope             char(1) = 'T',
  4913.                  @nullable          char(1) = 'U',
  4914.                  @ODBCVer           int = 2)
  4915. AS
  4916.     DECLARE @indid              int
  4917.     DECLARE @table_id           int
  4918.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4919.     DECLARE @scopeout           smallint
  4920.  
  4921.     if @col_type not in ('R','V') or @col_type is null
  4922.     begin
  4923.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  4924.         return
  4925.     end
  4926.  
  4927.     if @scope = 'C'
  4928.         select @scopeout = 0
  4929.     else if @scope = 'T'
  4930.         select @scopeout = 1
  4931.     else
  4932.     begin
  4933.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4934.         return
  4935.     end
  4936.  
  4937.     if @nullable not in ('U','O') or @nullable is null
  4938.     begin
  4939.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4940.         return
  4941.     end
  4942.  
  4943.     if @table_qualifier is not null
  4944.     begin
  4945.         if db_name() <> @table_qualifier
  4946.         begin /* If qualifier doesn't match current database */
  4947.             raiserror (15250, -1,-1)
  4948.             return
  4949.         end
  4950.     end
  4951.     if @table_owner is null
  4952.     begin    /* If unqualified table name */
  4953.         SELECT @full_table_name = @table_name
  4954.     end
  4955.     else
  4956.     begin    /* Qualified table name */
  4957.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4958.     end
  4959.     /*  Get Object ID */
  4960.     SELECT @table_id = object_id(@full_table_name)
  4961.  
  4962.     if @col_type = 'V'
  4963.     BEGIN /* if ROWVER, just run that query */
  4964.         SELECT
  4965.             SCOPE = convert(smallint,NULL),
  4966.             COLUMN_NAME = convert(varchar(32),c.name),
  4967.             DATA_TYPE = convert(smallint, -2),
  4968.             TYPE_NAME = t.name,
  4969.             "PRECISION" = convert(int,8),
  4970.             LENGTH = convert(int,8),
  4971.             SCALE = convert(smallint, NULL),
  4972.             PSEUDO_COLUMN = convert(smallint,1)
  4973.         FROM
  4974.             systypes t, syscolumns c
  4975.         WHERE
  4976.             c.id = @table_id
  4977.             AND c.usertype = 80 /*  TIMESTAMP */
  4978.             AND t.usertype = 80 /*  TIMESTAMP */
  4979.         RETURN
  4980.     END
  4981.  
  4982.     /* ROWID, now find the id of the 'best' index for this table */
  4983.  
  4984.     IF @nullable = 'O'  /* Don't include any indexes that contain
  4985.                            nullable columns. */
  4986.  
  4987.         SELECT @indid = MIN(indid)
  4988.             FROM sysindexes i,syscolumns c,syscolumns c2
  4989.             WHERE
  4990.                 i.status&2 = 2      /*  If Unique Index */
  4991.                 AND c.id = i.id
  4992.                 AND c2.id = c.id
  4993.                 AND c2.colid < i.keycnt + (i.status&16)/16
  4994.                 AND i.id = @table_id
  4995.                 AND indid > 0       /*  Eliminate Table Row */
  4996.                 AND c.name = index_col(@table_name,i.indid,c2.colid)
  4997.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  4998.  
  4999.     ELSE    /* Include indexes that are partially nullable. */
  5000.  
  5001.         SELECT @indid = MIN(indid)
  5002.             FROM sysindexes i
  5003.             WHERE
  5004.                 status&2 = 2        /*  If Unique Index */
  5005.                 AND id = @table_id
  5006.                 AND indid > 0       /*  Eliminate Table Row */
  5007.  
  5008.     SELECT
  5009.         SCOPE = @scopeout,
  5010.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  5011.         d.DATA_TYPE,
  5012.         convert(varchar(32),case
  5013.             when (t.usertype > 100 or t.usertype in (18,80))
  5014.                 then t.name
  5015.             else d.TYPE_NAME
  5016.         end) TYPE_NAME,
  5017.         convert(int,case
  5018.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5019.             else isnull(convert(int,c.prec), 2147483647)
  5020.         end) "PRECISION",
  5021.         convert(int,case
  5022.             when d.ss_dtype IN (106, 108, 55, 63) then  /* decimal/numeric types */
  5023.                 convert(int,c.prec+2)
  5024.             else
  5025.                 isnull(d.length, c.length)
  5026.         end) LENGTH,
  5027.         SCALE = convert(smallint, c.scale),
  5028.         PSEUDO_COLUMN = convert(smallint,1)
  5029.     FROM
  5030.         sysindexes x,
  5031.         syscolumns c,
  5032.         master.dbo.spt_datatype_info d,
  5033.         systypes t,
  5034.         syscolumns c2   /* Self-join to generate list of index columns and */
  5035.                         /* to extract datatype names */
  5036.     WHERE
  5037.         x.id = @table_id
  5038.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  5039.         AND c.id = x.id
  5040.         AND c2.id = x.id
  5041.         AND c2.colid < x.keycnt+(x.status&16)/16
  5042.         AND x.indid = @indid
  5043.         AND t.type = d.ss_dtype
  5044.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5045.         AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  5046.         AND c.usertype = t.usertype
  5047. go
  5048.  
  5049. if (charindex('7.00', @@version) = 0 and
  5050.     charindex('8.00', @@version) = 0)
  5051. begin
  5052.     print ''
  5053.     print ''
  5054.     print 'Warning:'
  5055.     print 'you are installing the stored procedures '
  5056.     print 'on a pre 7.0 SQL Server.'
  5057.     print 'Ignore the following errors.'
  5058. end
  5059. else
  5060.     drop proc sp_special_columns
  5061. go
  5062.  
  5063. /*  Procedure for 7.0 servers */
  5064. CREATE PROCEDURE sp_special_columns (
  5065.                  @table_name        sysname,
  5066.                  @table_owner       sysname = null,
  5067.                  @table_qualifier   sysname = null,
  5068.                  @col_type          char(1) = 'R',
  5069.                  @scope             char(1) = 'T',
  5070.                  @nullable          char(1) = 'U',
  5071.                  @ODBCVer           int = 2)
  5072. AS
  5073.     DECLARE @indid              int
  5074.     DECLARE @table_id           int
  5075.     DECLARE @full_table_name    nvarchar(257)
  5076.     DECLARE @scopeout           smallint
  5077.  
  5078.     if @col_type not in ('R','V') or @col_type is null
  5079.     begin
  5080.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  5081.         return
  5082.     end
  5083.  
  5084.     if @scope = 'C'
  5085.         select @scopeout = 0
  5086.     else if @scope = 'T'
  5087.         select @scopeout = 1
  5088.     else
  5089.     begin
  5090.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  5091.         return
  5092.     end
  5093.  
  5094.     if @nullable not in ('U','O') or @nullable is null
  5095.     begin
  5096.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  5097.         return
  5098.     end
  5099.  
  5100.     if @table_qualifier is not null
  5101.     begin
  5102.         if db_name() <> @table_qualifier
  5103.         begin /* If qualifier doesn't match current database */
  5104.             raiserror (15250, -1,-1)
  5105.             return
  5106.         end
  5107.     end
  5108.     if @table_owner is null
  5109.     begin    /* If unqualified table name */
  5110.         SELECT @full_table_name = quotename(@table_name)
  5111.     end
  5112.     else
  5113.     begin   /* Qualified table name */
  5114.         if @table_owner = ''
  5115.         begin   /* If empty owner name */
  5116.             SELECT @full_table_name = quotename(@table_owner)
  5117.         end
  5118.         else
  5119.         begin
  5120.             SELECT @full_table_name = quotename(@table_owner) +
  5121.                 '.' + quotename(@table_name)
  5122.         end
  5123.     end
  5124.     /*  Get Object ID */
  5125.     SELECT @table_id = object_id(@full_table_name)
  5126.  
  5127.     if @col_type = 'V'
  5128.     BEGIN /* if ROWVER, just run that query */
  5129.         SELECT
  5130.             SCOPE = convert(smallint,NULL),
  5131.             COLUMN_NAME = convert(sysname,c.name),
  5132.             DATA_TYPE = convert(smallint, -2),
  5133.             TYPE_NAME = t.name,
  5134.             "PRECISION" = convert(int,8),
  5135.             LENGTH = convert(int,8),
  5136.             SCALE = convert(smallint, NULL),
  5137.             PSEUDO_COLUMN = convert(smallint,1)
  5138.         FROM
  5139.             systypes t, syscolumns c
  5140.         WHERE
  5141.             not (@table_id is null)
  5142.             AND c.id = @table_id
  5143.             AND t.name = 'timestamp'    /*  TIMESTAMP  */
  5144.             AND t.xtype = c.xtype       
  5145.             AND t.xusertype = c.xusertype   
  5146.         RETURN
  5147.     END
  5148.  
  5149.     /* ROWID, now find the id of the 'best' index for this table */
  5150.  
  5151.     IF @nullable = 'O'  /* Don't include any indexes that contain
  5152.                            nullable columns. */
  5153.  
  5154.         SELECT @indid = MIN(indid)
  5155.             FROM sysindexes x, syscolumns c, syscolumns c2
  5156.             WHERE
  5157.                 not (@table_id is null)
  5158.                 AND x.status&2 = 2      /*  If Unique Index */
  5159.                 AND c.id = x.id
  5160.                 AND c2.id = c.id
  5161.                 AND c2.colid < x.keycnt + (x.status&16)/16
  5162.                 AND x.id = @table_id
  5163.                 AND indid > 0       /*  Eliminate Table Row */
  5164.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  5165.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  5166.  
  5167.     ELSE    /* Include indexes that are partially nullable. */
  5168.  
  5169.         SELECT @indid = MIN(indid)
  5170.             FROM sysindexes x
  5171.             WHERE
  5172.                 not (@table_id is null)
  5173.                 AND status&2 = 2        /*  If Unique Index */
  5174.                 AND id = @table_id
  5175.                 AND indid > 0       /*  Eliminate Table Row */
  5176.  
  5177.     SELECT
  5178.         SCOPE = @scopeout,
  5179.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  5180.         d.DATA_TYPE,
  5181.         convert(sysname,case
  5182.             when t.xusertype > 255 then t.name
  5183.             else d.TYPE_NAME
  5184.         end) TYPE_NAME,
  5185.         convert(int,case
  5186.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5187.             else OdbcPrec(c.xtype,c.length,c.xprec)
  5188.         end) "PRECISION",
  5189.         convert(int,case
  5190.             when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5191.                 OdbcPrec(c.xtype,c.length,c.xprec)+2
  5192.             else isnull(d.length, c.length)
  5193.         end) LENGTH,
  5194.         SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5195.         PSEUDO_COLUMN = convert(smallint,1)
  5196.     FROM
  5197.         sysindexes x,
  5198.         syscolumns c,
  5199.         master.dbo.spt_datatype_info d,
  5200.         systypes t,
  5201.         syscolumns c2   /* Self-join to generate list of index columns and */
  5202.                         /* to extract datatype names */
  5203.     WHERE
  5204.         not (@table_id is null)
  5205.         AND x.id = @table_id
  5206.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  5207.         AND c.id = x.id
  5208.         AND c2.id = x.id
  5209.         AND c2.colid < x.keycnt + (x.status&16)/16
  5210.         AND x.indid = @indid
  5211.         AND t.xtype = d.ss_dtype
  5212.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5213.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  5214.         AND c.xusertype = t.xusertype
  5215. go
  5216.  
  5217. if (charindex('8.00', @@version) = 0)
  5218. begin
  5219.     print ''
  5220.     print ''
  5221.     print 'Warning:'
  5222.     print 'you are installing the stored procedures '
  5223.     print 'on a pre 8.0 SQL Server.'
  5224.     print 'Ignore the following errors.'
  5225. end
  5226. else
  5227.     drop proc sp_special_columns
  5228. go
  5229.  
  5230. /*  Procedure for 8.0 and later servers */
  5231. CREATE PROCEDURE sp_special_columns (
  5232.                  @table_name        sysname,
  5233.                  @table_owner       sysname = null,
  5234.                  @table_qualifier   sysname = null,
  5235.                  @col_type          char(1) = 'R',
  5236.                  @scope             char(1) = 'T',
  5237.                  @nullable          char(1) = 'U',
  5238.                  @ODBCVer           int = 2)
  5239. AS
  5240.     DECLARE @indid              int
  5241.     DECLARE @table_id           int
  5242.     DECLARE @full_table_name    nvarchar(257)
  5243.     DECLARE @scopeout           smallint
  5244.  
  5245.     if @col_type not in ('R','V') or @col_type is null
  5246.     begin
  5247.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  5248.         return
  5249.     end
  5250.  
  5251.     if @scope = 'C'
  5252.         select @scopeout = 0
  5253.     else if @scope = 'T'
  5254.         select @scopeout = 1
  5255.     else
  5256.     begin
  5257.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  5258.         return
  5259.     end
  5260.  
  5261.     if @nullable not in ('U','O') or @nullable is null
  5262.     begin
  5263.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  5264.         return
  5265.     end
  5266.  
  5267.     if @table_qualifier is not null
  5268.     begin
  5269.         if db_name() <> @table_qualifier
  5270.         begin /* If qualifier doesn't match current database */
  5271.             raiserror (15250, -1,-1)
  5272.             return
  5273.         end
  5274.     end
  5275.     if @table_owner is null
  5276.     begin    /* If unqualified table name */
  5277.         SELECT @full_table_name = quotename(@table_name)
  5278.     end
  5279.     else
  5280.     begin   /* Qualified table name */
  5281.         if @table_owner = ''
  5282.         begin   /* If empty owner name */
  5283.             SELECT @full_table_name = quotename(@table_owner)
  5284.         end
  5285.         else
  5286.         begin
  5287.             SELECT @full_table_name = quotename(@table_owner) +
  5288.                 '.' + quotename(@table_name)
  5289.         end
  5290.     end
  5291.     /*  Get Object ID */
  5292.     SELECT @table_id = object_id(@full_table_name)
  5293.  
  5294.     if @col_type = 'V'
  5295.     BEGIN /* if ROWVER, just run that query */
  5296.         SELECT
  5297.             SCOPE = convert(smallint,NULL),
  5298.             COLUMN_NAME = convert(sysname,c.name),
  5299.             DATA_TYPE = convert(smallint, -2),
  5300.             TYPE_NAME = t.name,
  5301.             "PRECISION" = convert(int,8),
  5302.             LENGTH = convert(int,8),
  5303.             SCALE = convert(smallint, NULL),
  5304.             PSEUDO_COLUMN = convert(smallint,1)
  5305.         FROM
  5306.             systypes t, syscolumns c
  5307.         WHERE
  5308.             not (@table_id is null)
  5309.             AND c.id = @table_id
  5310.             AND t.name = 'timestamp'    /*  TIMESTAMP  */
  5311.             AND t.xtype = c.xtype       
  5312.             AND t.xusertype = c.xusertype   
  5313.         RETURN
  5314.     END
  5315.  
  5316.     /* ROWID, now find the id of the 'best' index for this table */
  5317.  
  5318.     IF @nullable = 'O'  /* Don't include any indexes that contain
  5319.                            nullable columns. */
  5320.  
  5321.         SELECT @indid = MIN(indid)
  5322.             FROM sysindexes x, syscolumns c, syscolumns c2
  5323.             WHERE
  5324.                 not (@table_id is null)
  5325.                 AND x.status&2 = 2      /*  If Unique Index */
  5326.                 AND c.id = x.id
  5327.                 AND c2.id = c.id
  5328.                 AND c2.colid < x.keycnt + (x.status&18)/18
  5329.                 AND x.id = @table_id
  5330.                 AND indid > 0       /*  Eliminate Table Row */
  5331.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  5332.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  5333.  
  5334.     ELSE    /* Include indexes that are partially nullable. */
  5335.  
  5336.         SELECT @indid = MIN(indid)
  5337.             FROM sysindexes x
  5338.             WHERE
  5339.                 not (@table_id is null)
  5340.                 AND status&2 = 2        /*  If Unique Index */
  5341.                 AND id = @table_id
  5342.                 AND indid > 0       /*  Eliminate Table Row */
  5343.  
  5344.     SELECT
  5345.         SCOPE = @scopeout,
  5346.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  5347.         d.DATA_TYPE,
  5348.         convert(sysname,case
  5349.             when t.xusertype > 255 then t.name
  5350.             else d.TYPE_NAME collate database_default
  5351.         end) TYPE_NAME,
  5352.         convert(int,case
  5353.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5354.             else OdbcPrec(c.xtype,c.length,c.xprec)
  5355.         end) "PRECISION",
  5356.         convert(int,case
  5357.             when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5358.                 OdbcPrec(c.xtype,c.length,c.xprec)+2
  5359.             else isnull(d.length, c.length)
  5360.         end) LENGTH,
  5361.         SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5362.         PSEUDO_COLUMN = convert(smallint,1)
  5363.     FROM
  5364.         sysindexes x,
  5365.         syscolumns c,
  5366.         master.dbo.spt_datatype_info d,
  5367.         systypes t,
  5368.         syscolumns c2   /* Self-join to generate list of index columns and */
  5369.                         /* to extract datatype names */
  5370.     WHERE
  5371.         not (@table_id is null)
  5372.         AND x.id = @table_id
  5373.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  5374.         AND c.id = x.id
  5375.         AND c2.id = x.id
  5376.         AND c2.colid < x.keycnt + (x.status&16)/16
  5377.         AND x.indid = @indid
  5378.         AND t.xtype = d.ss_dtype
  5379.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5380.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  5381.         AND c.xusertype = t.xusertype
  5382. go
  5383.  
  5384. grant execute on sp_special_columns to public
  5385. go
  5386.  
  5387. dump tran master with no_log
  5388. go
  5389.  
  5390. print 'creating sp_sproc_columns'
  5391. go
  5392.  
  5393. /*  Procedure for pre-6.0 server */
  5394. CREATE PROCEDURE sp_sproc_columns (
  5395.                  @procedure_name        varchar(102) = '%', /* 3*32+5+1 */
  5396.                  @procedure_owner       varchar(96) = null,
  5397.                  @procedure_qualifier   varchar(32) = null,
  5398.                  @column_name           varchar(96) = null,
  5399.                  @ODBCVer               int = 2)
  5400. AS
  5401.     DECLARE @group_num_lower smallint
  5402.     DECLARE @group_num_upper smallint
  5403.     DECLARE @semi_position int
  5404.     DECLARE @full_procedure_name    varchar(205)/* 2*102+1 */
  5405.     DECLARE @procedure_id int
  5406.  
  5407.     if @column_name is null /*  If column name not supplied, match all */
  5408.         select @column_name = '%'
  5409.     if @procedure_qualifier is not null
  5410.     begin
  5411.         if db_name() <> @procedure_qualifier
  5412.         begin
  5413.             if @procedure_qualifier = ''
  5414.             begin
  5415.                 /* in this case, we need to return an empty result set */
  5416.                 /* because the user has requested a database with an empty name */
  5417.                 select @procedure_name = ''
  5418.                 select @procedure_owner = ''
  5419.             end
  5420.             else
  5421.             begin   /* If qualifier doesn't match current database */
  5422.                 raiserror 20001 '~~Rush_51~~'
  5423.                 return
  5424.             end
  5425.         end
  5426.     end
  5427.  
  5428.     if @procedure_name is null
  5429.     begin   /*  If procedure name not supplied, match all */
  5430.         select @procedure_name = '%'
  5431.     end
  5432.  
  5433.     /* first we need to extract the procedure group number, if one exists */
  5434.     select @semi_position = charindex(';',@procedure_name)
  5435.     if (@semi_position > 0)
  5436.     begin   /* If group number separator (;) found */
  5437.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5438.         select @group_num_upper = @group_num_lower
  5439.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5440.     end
  5441.     else
  5442.     begin   /* No group separator, so default to group number of 1 */
  5443.         select @group_num_lower = 1
  5444.         select @group_num_upper = 32767         
  5445.     end
  5446.  
  5447.     if @procedure_owner is null
  5448.     begin   /* If unqualified procedure name */
  5449.         SELECT @full_procedure_name = @procedure_name
  5450.     end
  5451.     else
  5452.     begin   /* Qualified procedure name */
  5453.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  5454.     end
  5455.  
  5456.     /*  Get Object ID */
  5457.     SELECT @procedure_id = object_id(@full_procedure_name)
  5458.     if ((charindex('%',@full_procedure_name) = 0) and
  5459.         (charindex('_',@full_procedure_name) = 0) and
  5460.         @procedure_id <> 0)
  5461.     begin
  5462.         /* this block is for the case where there is no pattern
  5463.             matching required for the procedure name */
  5464.         SELECT
  5465.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5466.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5467.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5468.             COLUMN_NAME = convert(varchar(32),c.name),
  5469.             COLUMN_TYPE = convert(smallint, 0),
  5470.             d.DATA_TYPE,
  5471.             TYPE_NAME = t.name,
  5472.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  5473.             LENGTH = isnull(d.length, convert(int,c.length)),
  5474.             SCALE = d.numeric_scale,
  5475.             d.RADIX,
  5476.             d.NULLABLE,
  5477.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5478.             COLUMN_DEF = convert(varchar(255),null),
  5479.             d.SQL_DATA_TYPE,
  5480.             d.SQL_DATETIME_SUB,
  5481.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  5482.             ORDINAL_POSITION = convert(int, c.colid),
  5483.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5484.             SS_DATA_TYPE = c.type
  5485.         FROM
  5486.             syscolumns c,
  5487.             sysobjects o,
  5488.             master.dbo.spt_datatype_info d,
  5489.             systypes t
  5490.         WHERE
  5491.             o.id = @procedure_id
  5492.             AND c.id = o.id
  5493.             AND t.type = d.ss_dtype
  5494.             AND c.length = isnull(d.fixlen, c.length)
  5495.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5496.             AND c.usertype = t.usertype
  5497.             AND c.name like @column_name
  5498.             AND c.number between @group_num_lower and @group_num_upper
  5499.         UNION ALL
  5500.         SELECT         /* return value row*/
  5501.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5502.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5503.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5504.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5505.             COLUMN_TYPE = convert(smallint, 5),
  5506.             DATA_TYPE = convert(smallint, 4),
  5507.             TYPE_NAME = convert(varchar(32),'int'),
  5508.             "PRECISION" = convert(int,10),
  5509.             LENGTH = convert(int,4),
  5510.             SCALE = convert(smallint,0),
  5511.             RADIX = convert(smallint,10),
  5512.             NULLABLE = convert(smallint,0),
  5513.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5514.             COLUMN_DEF = convert(varchar(255),NULL),
  5515.             SQL_DATA_TYPE = convert(smallint, 4),
  5516.             SQL_DATETIME_SUB = convert(smallint,null),
  5517.             CHAR_OCTET_LENGTH = convert(int,null),
  5518.             ORDINAL_POSITION = convert(int,0),
  5519.             IS_NULLABLE = convert(varchar(254),'NO'),
  5520.             SS_DATA_TYPE = convert(tinyint,56)
  5521.         FROM
  5522.             syscomments c, sysobjects o
  5523.         WHERE
  5524.             o.id = @procedure_id
  5525.             AND c.id = o.id
  5526.             AND c.colid = 1
  5527.             AND o.type = 'P'                        /* Just Procedures */
  5528.             AND '@RETURN_VALUE' like @column_name
  5529.             AND c.number between @group_num_lower and @group_num_upper      
  5530.         ORDER BY 1, 2, 3, 18
  5531.     end
  5532.     else
  5533.     begin
  5534.         /* this block is for the case where there IS pattern
  5535.             matching done on the procedure name */
  5536.         if @procedure_owner is null
  5537.             select @procedure_owner = '%'
  5538.         SELECT
  5539.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5540.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5541.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5542.             COLUMN_NAME = convert(varchar(32),c.name),
  5543.             COLUMN_TYPE = convert(smallint, 0),
  5544.             d.DATA_TYPE,
  5545.             TYPE_NAME = t.name,
  5546.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  5547.             LENGTH = isnull(d.length, convert(int,c.length)),
  5548.             SCALE = d.numeric_scale,
  5549.             d.RADIX,
  5550.             d.NULLABLE,
  5551.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5552.             COLUMN_DEF = convert(varchar(255),null),
  5553.             d.SQL_DATA_TYPE,
  5554.             d.SQL_DATETIME_SUB,
  5555.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  5556.             ORDINAL_POSITION = convert(int, c.colid),
  5557.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5558.             SS_DATA_TYPE = c.type
  5559.         FROM
  5560.             syscolumns c,
  5561.             sysobjects o,
  5562.             master.dbo.spt_datatype_info d,
  5563.             systypes t
  5564.         WHERE
  5565.             o.name like @procedure_name
  5566.             AND user_name(o.uid) like @procedure_owner
  5567.             AND o.id = c.id
  5568.             AND t.type = d.ss_dtype
  5569.             AND c.length = isnull(d.fixlen, c.length)
  5570.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5571.             AND c.usertype = t.usertype
  5572.             AND o.type = 'P'                            /* Just Procedures */
  5573.             AND c.name like @column_name
  5574.             AND c.number between @group_num_lower and @group_num_upper
  5575.         UNION ALL
  5576.         SELECT         /* return value row*/
  5577.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5578.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5579.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5580.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5581.             COLUMN_TYPE = convert(smallint, 5),
  5582.             DATA_TYPE = convert(smallint, 4),
  5583.             TYPE_NAME = convert(varchar(32),'int'),
  5584.             "PRECISION" = convert(int,10),
  5585.             LENGTH = convert(int,4),
  5586.             SCALE = convert(smallint,0),
  5587.             RADIX = convert(smallint,10),
  5588.             NULLABLE = convert(smallint,0),
  5589.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5590.             COLUMN_DEF = convert(varchar(255),NULL),
  5591.             SQL_DATA_TYPE = convert(smallint, 4),
  5592.             SQL_DATETIME_SUB = convert(smallint,null),
  5593.             CHAR_OCTET_LENGTH = convert(int,null),
  5594.             ORDINAL_POSITION = convert(int,0),
  5595.             IS_NULLABLE = convert(varchar(254),'NO'),
  5596.             SS_DATA_TYPE = convert(tinyint,56)
  5597.         FROM
  5598.             syscomments c, sysobjects o
  5599.         WHERE
  5600.             o.name like @procedure_name
  5601.             AND user_name(o.uid) like @procedure_owner
  5602.             AND c.id = o.id
  5603.             AND c.colid = 1
  5604.             AND o.type = 'P'                        /* Just Procedures */
  5605.             AND '@RETURN_VALUE' like @column_name
  5606.             AND c.number between @group_num_lower and @group_num_upper
  5607.         ORDER BY 1, 2, 3, 18
  5608.     end
  5609. go
  5610.  
  5611. if (charindex('6.00', @@version) = 0 and
  5612.     charindex('6.50', @@version) = 0 and
  5613.     charindex('7.00', @@version) = 0 and
  5614.     charindex('8.00', @@version) = 0)
  5615. begin
  5616.     print ''
  5617.     print ''
  5618.     print 'Warning:'
  5619.     print 'you are installing the stored procedures '
  5620.     print 'on a pre 6.0 SQL Server.'
  5621.     print 'Ignore the following error.'
  5622. end
  5623. else
  5624.     drop proc sp_sproc_columns
  5625. go
  5626.  
  5627. /*  Procedure for 6.0 and 6.50 servers */
  5628. CREATE PROCEDURE sp_sproc_columns (
  5629.                  @procedure_name        varchar(102) = '%', /* 3*36+5+1 */
  5630.                  @procedure_owner       varchar(96) = null,
  5631.                  @procedure_qualifier   varchar(32) = null,
  5632.                  @column_name           varchar(96) = null,
  5633.                  @ODBCVer               int = 2)
  5634. AS
  5635.     DECLARE @group_num_lower smallint
  5636.     DECLARE @group_num_upper smallint
  5637.     DECLARE @semi_position int
  5638.     DECLARE @full_procedure_name    varchar(205)
  5639.     DECLARE @procedure_id int
  5640.  
  5641.     if @column_name is null /*  If column name not supplied, match all */
  5642.         select @column_name = '%'
  5643.     if @procedure_qualifier is not null
  5644.     begin
  5645.         if db_name() <> @procedure_qualifier
  5646.         begin
  5647.             if @procedure_qualifier = ''
  5648.             begin
  5649.                 /* in this case, we need to return an empty result set */
  5650.                 /* because the user has requested a database with an empty name */
  5651.                 select @procedure_name = ''
  5652.                 select @procedure_owner = ''
  5653.             end
  5654.             else
  5655.             begin   /* If qualifier doesn't match current database */
  5656.                 raiserror (15250, -1,-1)
  5657.                 return
  5658.             end
  5659.         end
  5660.     end
  5661.  
  5662.     if @procedure_name is null
  5663.     begin   /*  If procedure name not supplied, match all */
  5664.         select @procedure_name = '%'
  5665.     end
  5666.  
  5667.     /* first we need to extract the procedure group number, if one exists */
  5668.     select @semi_position = charindex(';',@procedure_name)
  5669.     if (@semi_position > 0)
  5670.     begin   /* If group number separator (;) found */
  5671.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5672.         select @group_num_upper = @group_num_lower
  5673.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5674.     end
  5675.     else
  5676.     begin   /* No group separator, so default to group number of 1 */
  5677.         select @group_num_lower = 1
  5678.         select @group_num_upper = 32767         
  5679.     end
  5680.  
  5681.     if @procedure_owner is null
  5682.     begin   /* If unqualified procedure name */
  5683.         SELECT @full_procedure_name = @procedure_name
  5684.     end
  5685.     else
  5686.     begin   /* Qualified procedure name */
  5687.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  5688.     end
  5689.  
  5690.     /*  Get Object ID */
  5691.     SELECT @procedure_id = object_id(@full_procedure_name)
  5692.     if ((charindex('%',@full_procedure_name) = 0) and
  5693.         (charindex('[',@full_procedure_name) = 0) and
  5694.         (charindex('_',@full_procedure_name) = 0) and
  5695.         @procedure_id <> 0)
  5696.     begin
  5697.         /* this block is for the case where there is no pattern
  5698.             matching required for the procedure name */
  5699.         SELECT
  5700.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5701.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5702.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5703.             COLUMN_NAME = convert(varchar(32),c.name),
  5704.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  5705.             d.DATA_TYPE,
  5706.             TYPE_NAME = t.name,
  5707.             convert(int,case
  5708.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5709.                 else isnull(convert(int,c.prec), 2147483647)
  5710.             end) "PRECISION",
  5711.             convert(int,case
  5712.                 when d.ss_dtype IN (106, 108, 55, 63) then  /* decimal/numeric types */
  5713.                     c.prec+2
  5714.                 else
  5715.                     isnull(d.length, c.length)
  5716.             end) LENGTH,
  5717.             SCALE = convert(smallint, c.scale),
  5718.             d.RADIX,
  5719.             d.NULLABLE,
  5720.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5721.             COLUMN_DEF = convert(varchar(255),NULL),
  5722.             d.SQL_DATA_TYPE,
  5723.             d.SQL_DATETIME_SUB,
  5724.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  5725.             ORDINAL_POSITION = convert(int, c.colid),
  5726.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5727.             SS_DATA_TYPE = c.type
  5728.         FROM
  5729.             syscolumns c,
  5730.             sysobjects o,
  5731.             master.dbo.spt_datatype_info d,
  5732.             systypes t
  5733.         WHERE
  5734.             o.id = @procedure_id
  5735.             AND c.id = o.id
  5736.             AND c.type = d.ss_dtype
  5737.             AND c.length = isnull(d.fixlen, c.length)
  5738.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5739.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5740.             AND c.usertype = t.usertype
  5741.             AND c.name like @column_name
  5742.             AND c.number between @group_num_lower and @group_num_upper
  5743.         UNION ALL
  5744.         SELECT         /* return value row*/
  5745.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5746.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5747.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5748.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5749.             COLUMN_TYPE = convert(smallint, 5),
  5750.             DATA_TYPE = convert(smallint, 4),
  5751.             TYPE_NAME = convert(varchar(32),'int'),
  5752.             "PRECISION" = convert(int,10),
  5753.             LENGTH = convert(int,4),
  5754.             SCALE = convert(smallint,0),
  5755.             RADIX = convert(smallint,10),
  5756.             NULLABLE = convert(smallint,0),
  5757.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5758.             COLUMN_DEF = convert(varchar(255),NULL),
  5759.             SQL_DATA_TYPE = convert(smallint, 4),
  5760.             SQL_DATETIME_SUB = convert(smallint,null),
  5761.             CHAR_OCTET_LENGTH = convert(int,null),
  5762.             ORDINAL_POSITION = convert(int,0),
  5763.             IS_NULLABLE = convert(varchar(254),'NO'),
  5764.             SS_DATA_TYPE = convert(tinyint,56)
  5765.         FROM
  5766.             syscomments c, sysobjects o
  5767.         WHERE
  5768.             o.id = @procedure_id
  5769.             AND c.id = o.id
  5770.             AND c.colid = 1
  5771.             AND o.type = 'P'                        /* Just Procedures */
  5772.             AND '@RETURN_VALUE' like @column_name
  5773.             AND c.number between @group_num_lower and @group_num_upper
  5774.         ORDER BY 1, 2, 3, 18
  5775.     end
  5776.     else
  5777.     begin
  5778.         /* this block is for the case where there IS pattern
  5779.             matching done on the procedure name */
  5780.         if @procedure_owner is null
  5781.             select @procedure_owner = '%'
  5782.         SELECT
  5783.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5784.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5785.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5786.             COLUMN_NAME = convert(varchar(32),c.name),
  5787.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  5788.             d.DATA_TYPE,
  5789.             TYPE_NAME = t.name,
  5790.             convert(int,case
  5791.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5792.                 else isnull(convert(int,c.prec), 2147483647)
  5793.             end) "PRECISION",
  5794.             convert(int,case
  5795.                 when d.ss_dtype IN (106, 108, 55, 63) then  /* decimal/numeric types */
  5796.                     c.prec+2
  5797.                 else
  5798.                     isnull(d.length, c.length)
  5799.             end) LENGTH,
  5800.             SCALE = convert(smallint, c.scale),
  5801.             d.RADIX,
  5802.             d.NULLABLE,
  5803.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5804.             COLUMN_DEF = convert(varchar(255),NULL),
  5805.             d.SQL_DATA_TYPE,
  5806.             d.SQL_DATETIME_SUB,
  5807.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  5808.             ORDINAL_POSITION = convert(int, c.colid),
  5809.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5810.             SS_DATA_TYPE = c.type
  5811.         FROM
  5812.             syscolumns c,
  5813.             sysobjects o,
  5814.             master.dbo.spt_datatype_info d,
  5815.             systypes t
  5816.         WHERE
  5817.             o.name like @procedure_name
  5818.             AND user_name(o.uid) like @procedure_owner
  5819.             AND o.id = c.id
  5820.             AND c.type = d.ss_dtype
  5821.             AND c.length = isnull(d.fixlen, c.length)
  5822.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5823.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5824.             AND c.usertype = t.usertype
  5825.             AND o.type = 'P'                            /* Just Procedures */
  5826.             AND c.name like @column_name
  5827.             AND c.number between @group_num_lower and @group_num_upper
  5828.         UNION ALL
  5829.         SELECT         /* return value row*/
  5830.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5831.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5832.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5833.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5834.             COLUMN_TYPE = convert(smallint, 5),
  5835.             DATA_TYPE = convert(smallint, 4),
  5836.             TYPE_NAME = convert(varchar(32),'int'),
  5837.             "PRECISION" = convert(int,10),
  5838.             LENGTH = convert(int,4),
  5839.             SCALE = convert(smallint,0),
  5840.             RADIX = convert(smallint,10),
  5841.             NULLABLE = convert(smallint,0),
  5842.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5843.             COLUMN_DEF = convert(varchar(255),NULL),
  5844.             SQL_DATA_TYPE = convert(smallint, 4),
  5845.             SQL_DATETIME_SUB = convert(smallint,null),
  5846.             CHAR_OCTET_LENGTH = convert(int,null),
  5847.             ORDINAL_POSITION = convert(int,0),
  5848.             IS_NULLABLE = convert(varchar(254),'NO'),
  5849.             SS_DATA_TYPE = convert(tinyint,56)
  5850.         FROM
  5851.             syscomments c, sysobjects o
  5852.         WHERE
  5853.             o.name like @procedure_name
  5854.             AND user_name(o.uid) like @procedure_owner
  5855.             AND c.id = o.id
  5856.             AND c.colid = 1
  5857.             AND o.type = 'P'                        /* Just Procedures */
  5858.             AND '@RETURN_VALUE' like @column_name
  5859.             AND c.number between @group_num_lower and @group_num_upper
  5860.         ORDER BY 1, 2, 3, 18
  5861.     end
  5862. go
  5863.  
  5864. if (charindex('7.00', @@version) = 0 and
  5865.     charindex('8.00', @@version) = 0)
  5866. begin
  5867.     print ''
  5868.     print ''
  5869.     print 'Warning:'
  5870.     print 'you are installing the stored procedures '
  5871.     print 'on a pre 7.0 SQL Server.'
  5872.     print 'Ignore the following errors.'
  5873. end
  5874. else
  5875.     drop proc sp_sproc_columns
  5876. go
  5877.  
  5878. /*  Procedure for 7.0 server */
  5879. CREATE PROCEDURE sp_sproc_columns (
  5880.                  @procedure_name        nvarchar(390) = '%',
  5881.                  @procedure_owner       nvarchar(384) = null,
  5882.                  @procedure_qualifier   sysname = null,
  5883.                  @column_name           nvarchar(384) = null,
  5884.                  @ODBCVer               int = 2)
  5885. AS
  5886.     DECLARE @group_num_lower smallint
  5887.     DECLARE @group_num_upper smallint
  5888.     DECLARE @semi_position int
  5889.     DECLARE @full_procedure_name    nvarchar(774)
  5890.     DECLARE @procedure_id int
  5891.  
  5892.     if @column_name is null /*  If column name not supplied, match all */
  5893.         select @column_name = '%'
  5894.     if @procedure_qualifier is not null
  5895.     begin
  5896.         if db_name() <> @procedure_qualifier
  5897.         begin
  5898.             if @procedure_qualifier = ''
  5899.             begin
  5900.                 /* in this case, we need to return an empty result set */
  5901.                 /* because the user has requested a database with an empty name */
  5902.                 select @procedure_name = ''
  5903.                 select @procedure_owner = ''
  5904.             end
  5905.             else
  5906.             begin   /* If qualifier doesn't match current database */
  5907.                 raiserror (15250, -1,-1)
  5908.                 return
  5909.             end
  5910.         end
  5911.     end
  5912.  
  5913.     if @procedure_name is null
  5914.     begin   /*  If procedure name not supplied, match all */
  5915.         select @procedure_name = '%'
  5916.     end
  5917.  
  5918.     /* first we need to extract the procedure group number, if one exists */
  5919.     select @semi_position = charindex(';',@procedure_name)
  5920.     if (@semi_position > 0)
  5921.     begin   /* If group number separator (;) found */
  5922.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5923.         select @group_num_upper = @group_num_lower
  5924.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5925.     end
  5926.     else
  5927.     begin   /* No group separator, so default to all groups */
  5928.         select @group_num_lower = 1
  5929.         select @group_num_upper = 32767         
  5930.     end
  5931.  
  5932.     if @procedure_owner is null
  5933.     begin   /* If unqualified procedure name */
  5934.         SELECT @full_procedure_name = quotename(@procedure_name)
  5935.     end
  5936.     else
  5937.     begin   /* Qualified procedure name */
  5938.         if @procedure_owner = ''
  5939.         begin   /* If empty owner name */
  5940.             SELECT @full_procedure_name = quotename(@procedure_owner)
  5941.         end
  5942.         else
  5943.         begin
  5944.             SELECT @full_procedure_name = quotename(@procedure_owner) +
  5945.                 '.' + quotename(@procedure_name)
  5946.         end
  5947.     end
  5948.  
  5949.     /*  Get Object ID */
  5950.     SELECT @procedure_id = object_id(@full_procedure_name)
  5951.     if ((isnull(charindex('%', @full_procedure_name),0) = 0) and
  5952.         (isnull(charindex('[', @procedure_name),0) = 0) and
  5953.         (isnull(charindex('[', @procedure_owner),0) = 0) and
  5954.         (isnull(charindex('_', @full_procedure_name),0) = 0) and
  5955.         not (@procedure_id is null))
  5956.     begin
  5957.         /* this block is for the case where there is no pattern
  5958.             matching required for the procedure name */
  5959.         SELECT
  5960.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5961.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5962.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5963.             COLUMN_NAME = convert(sysname,c.name),
  5964.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  5965.             d.DATA_TYPE,
  5966.             TYPE_NAME = t.name,
  5967.             convert(int,case
  5968.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5969.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  5970.             end) "PRECISION",
  5971.             convert(int,case
  5972.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5973.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  5974.                 else
  5975.                     isnull(d.length, c.length)
  5976.             end) LENGTH,
  5977.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5978.             d.RADIX,
  5979.             d.NULLABLE,
  5980.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5981.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5982.             d.SQL_DATA_TYPE,
  5983.             d.SQL_DATETIME_SUB,
  5984.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  5985.             ORDINAL_POSITION = convert(int, c.colid),
  5986.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5987.             SS_DATA_TYPE = c.type
  5988.         FROM
  5989.             syscolumns c,
  5990.             sysobjects o,
  5991.             master.dbo.spt_datatype_info d,
  5992.             systypes t
  5993.         WHERE
  5994.             o.id = @procedure_id
  5995.             AND c.id = o.id
  5996.             AND c.xtype = d.ss_dtype
  5997.             AND c.length = isnull(d.fixlen, c.length)
  5998.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5999.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6000.             AND c.xusertype = t.xusertype
  6001.             AND c.name like @column_name
  6002.             AND c.number between @group_num_lower and @group_num_upper
  6003.         UNION ALL
  6004.         SELECT         /* return value row*/
  6005.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6006.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6007.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6008.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6009.             COLUMN_TYPE = convert(smallint, 5),
  6010.             DATA_TYPE = convert(smallint, 4),
  6011.             TYPE_NAME = convert(sysname,'int'),
  6012.             "PRECISION" = convert(int,10),
  6013.             LENGTH = convert(int,4),
  6014.             SCALE = convert(smallint,0),
  6015.             RADIX = convert(smallint,10),
  6016.             NULLABLE = convert(smallint,0),
  6017.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6018.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6019.             SQL_DATA_TYPE = convert(smallint, 4),
  6020.             SQL_DATETIME_SUB = convert(smallint,null),
  6021.             CHAR_OCTET_LENGTH = convert(int,null),
  6022.             ORDINAL_POSITION = convert(int,0),
  6023.             IS_NULLABLE = convert(varchar(254),'NO'),
  6024.             SS_DATA_TYPE = convert(tinyint,56)
  6025.         FROM
  6026.             syscomments c, sysobjects o
  6027.         WHERE
  6028.             o.id = @procedure_id
  6029.             AND c.id = o.id
  6030.             AND c.colid = 1
  6031.             AND o.type = 'P'                        /* Just Procedures */
  6032.             AND '@RETURN_VALUE' like @column_name
  6033.             AND c.number between @group_num_lower and @group_num_upper      
  6034.         ORDER BY 1, 2, 3, 18
  6035.     end
  6036.     else
  6037.     begin
  6038.         /* this block is for the case where there IS pattern
  6039.             matching done on the procedure name */
  6040.         if @procedure_owner is null
  6041.             select @procedure_owner = '%'
  6042.         SELECT
  6043.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6044.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6045.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6046.             COLUMN_NAME = convert(sysname,c.name),
  6047.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  6048.             d.DATA_TYPE,
  6049.             TYPE_NAME = t.name,
  6050.             convert(int,case
  6051.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6052.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6053.             end) "PRECISION",
  6054.             convert(int,case
  6055.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6056.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6057.                 else
  6058.                     isnull(d.length, c.length)
  6059.             end) LENGTH,
  6060.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6061.             d.RADIX,
  6062.             d.NULLABLE,
  6063.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6064.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6065.             d.SQL_DATA_TYPE,
  6066.             d.SQL_DATETIME_SUB,
  6067.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6068.             ORDINAL_POSITION = convert(int, c.colid),
  6069.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6070.             SS_DATA_TYPE = c.type
  6071.         FROM
  6072.             syscolumns c,
  6073.             sysobjects o,
  6074.             master.dbo.spt_datatype_info d,
  6075.             systypes t
  6076.         WHERE
  6077.             o.name like @procedure_name
  6078.             AND user_name(o.uid) like @procedure_owner
  6079.             AND o.id = c.id
  6080.             AND c.xtype = d.ss_dtype
  6081.             AND c.length = isnull(d.fixlen, c.length)
  6082.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6083.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6084.             AND c.xusertype = t.xusertype
  6085.             AND o.type = 'P'                            /* Just Procedures */
  6086.             AND c.name like @column_name
  6087.             AND c.number between @group_num_lower and @group_num_upper
  6088.         UNION ALL
  6089.         SELECT         /* return value row*/
  6090.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6091.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6092.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  6093.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6094.             COLUMN_TYPE = convert(smallint, 5),
  6095.             DATA_TYPE = convert(smallint, 4),
  6096.             TYPE_NAME = convert(sysname,'int'),
  6097.             "PRECISION" = convert(int,10),
  6098.             LENGTH = convert(int,4),
  6099.             SCALE = convert(smallint,0),
  6100.             RADIX = convert(smallint,10),
  6101.             NULLABLE = convert(smallint,0),
  6102.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6103.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6104.             SQL_DATA_TYPE = convert(smallint, 4),
  6105.             SQL_DATETIME_SUB = convert(smallint,null),
  6106.             CHAR_OCTET_LENGTH = convert(int,null),
  6107.             ORDINAL_POSITION = convert(int,0),
  6108.             IS_NULLABLE = convert(varchar(254),'NO'),
  6109.             SS_DATA_TYPE = convert(tinyint,56)
  6110.         FROM
  6111.             syscomments c, sysobjects o
  6112.         WHERE
  6113.             o.name like @procedure_name
  6114.             AND c.id = o.id
  6115.             AND user_name(o.uid) like @procedure_owner
  6116.             AND c.colid = 1
  6117.             AND o.type = 'P'                        /* Just Procedures */
  6118.             AND '@RETURN_VALUE' like @column_name
  6119.             AND c.number between @group_num_lower and @group_num_upper
  6120.         ORDER BY 1, 2, 3, 18
  6121.     end
  6122. go
  6123.  
  6124. if (charindex('8.00', @@version) = 0)
  6125. begin
  6126.     print ''
  6127.     print ''
  6128.     print 'Warning:'
  6129.     print 'you are installing the stored procedures '
  6130.     print 'on a pre 8.0 SQL Server.'
  6131.     print 'Ignore the following errors.'
  6132. end
  6133. else
  6134.     drop proc sp_sproc_columns
  6135. go
  6136.  
  6137. /*  Procedure for 8.0 server */
  6138. CREATE PROCEDURE sp_sproc_columns (
  6139.                  @procedure_name        nvarchar(390) = '%',
  6140.                  @procedure_owner       nvarchar(384) = null,
  6141.                  @procedure_qualifier       sysname = null,
  6142.                  @column_name           nvarchar(384) = null,
  6143.                  @ODBCVer               int = 2)
  6144. AS
  6145.     DECLARE @group_num_lower smallint
  6146.     DECLARE @group_num_upper smallint
  6147.     DECLARE @semi_position int
  6148.     DECLARE @full_procedure_name nvarchar(774)
  6149.     DECLARE @procedure_id int
  6150.  
  6151.     if @column_name is null /*  If column name not supplied, match all */
  6152.         select @column_name = '%'
  6153.     if @procedure_qualifier is not null
  6154.     begin
  6155.         if db_name() <> @procedure_qualifier
  6156.         begin
  6157.             if @procedure_qualifier = ''
  6158.             begin
  6159.                 /* in this case, we need to return an empty result set */
  6160.                 /* because the user has requested a database with an empty name */
  6161.                 select @procedure_name = ''
  6162.                 select @procedure_owner = ''
  6163.             end
  6164.             else
  6165.             begin   /* If qualifier doesn't match current database */
  6166.                 raiserror (15250, -1,-1)
  6167.                 return
  6168.             end
  6169.         end
  6170.     end
  6171.  
  6172.     if @procedure_name is null
  6173.     begin   /*  If procedure name not supplied, match all */
  6174.         select @procedure_name = '%'
  6175.     end
  6176.  
  6177.     /* first we need to extract the procedure group number, if one exists */
  6178.     select @semi_position = charindex(';',@procedure_name)
  6179.     if (@semi_position > 0)
  6180.     begin   /* If group number separator (;) found */
  6181.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  6182.         select @group_num_upper = @group_num_lower
  6183.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  6184.     end
  6185.     else
  6186.     begin   /* No group separator, so default to all groups */
  6187.         select @group_num_lower = 1
  6188.         select @group_num_upper = 32767         
  6189.     end
  6190.  
  6191.     if @procedure_owner is null
  6192.     begin   /* If unqualified procedure name */
  6193.         SELECT @full_procedure_name = quotename(@procedure_name)
  6194.     end
  6195.     else
  6196.     begin   /* Qualified procedure name */
  6197.         if @procedure_owner = ''
  6198.         begin   /* If empty owner name */
  6199.             SELECT @full_procedure_name = quotename(@procedure_owner)
  6200.         end
  6201.         else
  6202.         begin
  6203.             SELECT @full_procedure_name = quotename(@procedure_owner) +
  6204.                 '.' + quotename(@procedure_name)
  6205.         end
  6206.     end
  6207.  
  6208.     /*  Get Object ID */
  6209.     SELECT @procedure_id = object_id(@full_procedure_name)
  6210.     if ((isnull(charindex('%', @full_procedure_name),0) = 0) and
  6211.         (isnull(charindex('[', @procedure_name),0) = 0) and
  6212.         (isnull(charindex('[', @procedure_owner),0) = 0) and
  6213.         (isnull(charindex('_', @full_procedure_name),0) = 0) and
  6214.         not (@procedure_id is null))
  6215.     begin
  6216.         /* this block is for the case where there is no pattern
  6217.             matching required for the procedure name */
  6218.         SELECT
  6219.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6220.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6221.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6222.             COLUMN_NAME = convert(sysname,c.name),
  6223.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  6224.             d.DATA_TYPE,
  6225.             TYPE_NAME = t.name,
  6226.             convert(int,case
  6227.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6228.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6229.             end) "PRECISION",
  6230.             convert(int,case
  6231.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6232.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6233.                 else
  6234.                     isnull(d.length, c.length)
  6235.             end) LENGTH,
  6236.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6237.             d.RADIX,
  6238.             d.NULLABLE,
  6239.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6240.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6241.             d.SQL_DATA_TYPE,
  6242.             d.SQL_DATETIME_SUB,
  6243.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6244.             ORDINAL_POSITION = convert(int, c.colid),
  6245.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6246.             SS_DATA_TYPE = c.type
  6247.         FROM
  6248.             syscolumns c,
  6249.             sysobjects o,
  6250.             master.dbo.spt_datatype_info d,
  6251.             systypes t
  6252.         WHERE
  6253.             o.id = @procedure_id
  6254.             AND c.id = o.id
  6255.             AND c.xtype = d.ss_dtype
  6256.             AND c.length = isnull(d.fixlen, c.length)
  6257.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6258.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6259.             AND c.xusertype = t.xusertype
  6260.             AND c.name like @column_name
  6261.             AND (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  6262.             AND ((c.number between @group_num_lower and @group_num_upper)
  6263.                  OR (c.number = 0 and o.type = 'FN'))
  6264.         UNION ALL
  6265.         SELECT         /* return value row*/
  6266.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6267.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6268.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6269.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6270.             COLUMN_TYPE = convert(smallint, 5),
  6271.             DATA_TYPE = convert(smallint, 4),
  6272.             TYPE_NAME = convert(sysname,'int'),
  6273.             "PRECISION" = convert(int,10),
  6274.             LENGTH = convert(int,4),
  6275.             SCALE = convert(smallint,0),
  6276.             RADIX = convert(smallint,10),
  6277.             NULLABLE = convert(smallint,0),
  6278.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6279.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6280.             SQL_DATA_TYPE = convert(smallint, 4),
  6281.             SQL_DATETIME_SUB = convert(smallint,null),
  6282.             CHAR_OCTET_LENGTH = convert(int,null),
  6283.             ORDINAL_POSITION = convert(int,0),
  6284.             IS_NULLABLE = convert(varchar(254),'NO'),
  6285.             SS_DATA_TYPE = convert(tinyint,56)
  6286.         FROM
  6287.             syscomments c, sysobjects o
  6288.         WHERE
  6289.             o.id = @procedure_id
  6290.             AND c.id = o.id
  6291.             AND c.colid = 1
  6292.             AND o.type = 'P'            /* Procedures */
  6293.             AND '@RETURN_VALUE' like @column_name
  6294.             AND c.number between @group_num_lower and @group_num_upper
  6295.         UNION ALL
  6296.         SELECT      /* UDF return value */
  6297.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6298.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6299.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6300.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6301.             COLUMN_TYPE = convert(smallint, 5), /* SQL_RETURN_VALUE */
  6302.             d.DATA_TYPE,
  6303.             TYPE_NAME = t.name,
  6304.             convert(int,case
  6305.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6306.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6307.             end) "PRECISION",
  6308.             convert(int,case
  6309.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6310.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6311.                 else
  6312.                     isnull(d.length, c.length)
  6313.             end) LENGTH,
  6314.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6315.             d.RADIX,
  6316.             NULLABLE = convert(smallint, c.isnullable),
  6317.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6318.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6319.             d.SQL_DATA_TYPE,
  6320.             d.SQL_DATETIME_SUB,
  6321.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6322.             ORDINAL_POSITION = convert(int,0),
  6323.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6324.             SS_DATA_TYPE = c.type
  6325.         FROM
  6326.             syscolumns c,
  6327.             sysobjects o,
  6328.             master.dbo.spt_datatype_info d,
  6329.             systypes t
  6330.         WHERE
  6331.             o.name like @procedure_name
  6332.             AND o.id = c.id
  6333.             AND c.xtype = d.ss_dtype
  6334.             AND c.length = isnull(d.fixlen, c.length)
  6335.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6336.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6337.             AND c.xusertype = t.xusertype
  6338.             AND o.type = 'FN'           /* Scalar UDF */
  6339.             AND c.name like @column_name
  6340.             AND c.colid = 0
  6341.             AND c.number = 0
  6342.         UNION ALL
  6343.         SELECT      /* Table valued functions */
  6344.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6345.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6346.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';1'),
  6347.             COLUMN_NAME = convert(sysname,'@TABLE_RETURN_VALUE'),
  6348.             COLUMN_TYPE = convert(smallint, 3),
  6349.             DATA_TYPE = convert(smallint, NULL),
  6350.             TYPE_NAME = convert(sysname, 'table'),
  6351.             "PRECISION" = convert(int,0),
  6352.             LENGTH = convert(int,0),
  6353.             SCALE = convert(smallint,0),
  6354.             RADIX = convert(smallint,0),
  6355.             NULLABLE = convert(smallint,0),
  6356.             REMARKS = convert(varchar(254), 'Result table returned by table valued function'),
  6357.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6358.             SQL_DATA_TYPE = convert(smallint, NULL),
  6359.             SQL_DATETIME_SUB = convert(smallint,null),
  6360.             CHAR_OCTET_LENGTH = convert(int,null),
  6361.             ORDINAL_POSITION = convert(int,0),
  6362.             IS_NULLABLE = convert(varchar(254),'NO'),
  6363.             SS_DATA_TYPE = convert(tinyint,0)
  6364.         FROM
  6365.             syscomments c, sysobjects o
  6366.         WHERE
  6367.             o.id = @procedure_id
  6368.             AND c.id = o.id
  6369.             AND c.colid = 1
  6370.             AND o.type IN ('TF', 'IF')
  6371.             AND '@TABLE_RETURN_VALUE' like @column_name
  6372.             AND c.number = 0
  6373.         ORDER BY 1, 2, 3, 18
  6374.     end
  6375.     else
  6376.     begin
  6377.         /* this block is for the case where there IS pattern
  6378.             matching done on the procedure name */
  6379.         if @procedure_owner is null
  6380.             select @procedure_owner = '%'
  6381.  
  6382.         SELECT
  6383.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6384.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6385.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6386.             COLUMN_NAME = convert(sysname,c.name),
  6387.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  6388.             d.DATA_TYPE,
  6389.             TYPE_NAME = t.name,
  6390.             convert(int,case
  6391.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6392.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6393.             end) "PRECISION",
  6394.             convert(int,case
  6395.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6396.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6397.                 else
  6398.                     isnull(d.length, c.length)
  6399.             end) LENGTH,
  6400.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6401.             d.RADIX,
  6402.             d.NULLABLE,
  6403.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6404.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6405.             d.SQL_DATA_TYPE,
  6406.             d.SQL_DATETIME_SUB,
  6407.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6408.             ORDINAL_POSITION = convert(int, c.colid),
  6409.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6410.             SS_DATA_TYPE = c.type
  6411.         FROM
  6412.             syscolumns c,
  6413.             sysobjects o,
  6414.             master.dbo.spt_datatype_info d,
  6415.             systypes t
  6416.         WHERE
  6417.             o.name like @procedure_name
  6418.             AND user_name(o.uid) like @procedure_owner
  6419.             AND o.id = c.id
  6420.             AND c.xtype = d.ss_dtype
  6421.             AND c.length = isnull(d.fixlen, c.length)
  6422.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6423.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6424.             AND c.xusertype = t.xusertype
  6425.             AND c.name like @column_name
  6426.             AND (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  6427.             AND ((c.number between @group_num_lower and @group_num_upper)
  6428.                  OR (c.number = 0 and o.type = 'FN'))
  6429.         UNION ALL
  6430.         SELECT         /* return value row*/
  6431.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6432.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6433.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  6434.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6435.             COLUMN_TYPE = convert(smallint, 5), /* SQL_RETURN_VALUE */
  6436.             DATA_TYPE = convert(smallint, 4),
  6437.             TYPE_NAME = convert(sysname,'int'),
  6438.             "PRECISION" = convert(int,10),
  6439.             LENGTH = convert(int,4),
  6440.             SCALE = convert(smallint,0),
  6441.             RADIX = convert(smallint,10),
  6442.             NULLABLE = convert(smallint,0),
  6443.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6444.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6445.             SQL_DATA_TYPE = convert(smallint, 4),
  6446.             SQL_DATETIME_SUB = convert(smallint,null),
  6447.             CHAR_OCTET_LENGTH = convert(int,null),
  6448.             ORDINAL_POSITION = convert(int,0),
  6449.             IS_NULLABLE = convert(varchar(254),'NO'),
  6450.             SS_DATA_TYPE = convert(tinyint,56)
  6451.         FROM
  6452.             syscomments c, sysobjects o
  6453.         WHERE
  6454.             o.name like @procedure_name
  6455.             AND c.id = o.id
  6456.             AND user_name(o.uid) like @procedure_owner
  6457.             AND c.colid = 1
  6458.             AND o.type = 'P'                    /* Procedures */
  6459.             AND '@RETURN_VALUE' like @column_name
  6460.             AND c.number between @group_num_lower and @group_num_upper
  6461.         UNION ALL
  6462.         SELECT      /* UDF return value */
  6463.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6464.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6465.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6466.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6467.             COLUMN_TYPE = convert(smallint, 5), /* SQL_RETURN_VALUE */
  6468.             d.DATA_TYPE,
  6469.             TYPE_NAME = t.name,
  6470.             convert(int,case
  6471.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6472.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6473.             end) "PRECISION",
  6474.             convert(int,case
  6475.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6476.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6477.                 else
  6478.                     isnull(d.length, c.length)
  6479.             end) LENGTH,
  6480.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6481.             d.RADIX,
  6482.             NULLABLE = convert(smallint, c.isnullable),
  6483.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6484.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6485.             d.SQL_DATA_TYPE,
  6486.             d.SQL_DATETIME_SUB,
  6487.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6488.             ORDINAL_POSITION = convert(int,0),
  6489.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6490.             SS_DATA_TYPE = c.type
  6491.         FROM
  6492.             syscolumns c,
  6493.             sysobjects o,
  6494.             master.dbo.spt_datatype_info d,
  6495.             systypes t
  6496.         WHERE
  6497.             o.name like @procedure_name
  6498.             AND user_name(o.uid) like @procedure_owner
  6499.             AND o.id = c.id
  6500.             AND c.xtype = d.ss_dtype
  6501.             AND c.length = isnull(d.fixlen, c.length)
  6502.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6503.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6504.             AND c.xusertype = t.xusertype
  6505.             AND o.type = 'FN'           /* Scalar UDF */
  6506.             AND c.name like @column_name
  6507.             AND c.colid = 0
  6508.             AND c.number = 0
  6509.         UNION ALL
  6510.         SELECT      /* Table valued functions */
  6511.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6512.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6513.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';1'),
  6514.             COLUMN_NAME = convert(sysname,'@TABLE_RETURN_VALUE'),
  6515.             COLUMN_TYPE = convert(smallint, 3),
  6516.             DATA_TYPE = convert(smallint, NULL),
  6517.             TYPE_NAME = convert(sysname, 'table'),
  6518.             "PRECISION" = convert(int,0),
  6519.             LENGTH = convert(int,0),
  6520.             SCALE = convert(smallint,0),
  6521.             RADIX = convert(smallint,0),
  6522.             NULLABLE = convert(smallint,0),
  6523.             REMARKS = convert(varchar(254), 'Result table returned by table valued function'),
  6524.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6525.             SQL_DATA_TYPE = convert(smallint, NULL),
  6526.             SQL_DATETIME_SUB = convert(smallint,null),
  6527.             CHAR_OCTET_LENGTH = convert(int,null),
  6528.             ORDINAL_POSITION = convert(int,0),
  6529.             IS_NULLABLE = convert(varchar(254),'NO'),
  6530.             SS_DATA_TYPE = convert(tinyint,0)
  6531.         FROM
  6532.             syscomments c, sysobjects o
  6533.         WHERE
  6534.             o.name like @procedure_name
  6535.             AND user_name(o.uid) like @procedure_owner
  6536.             AND c.id = o.id
  6537.             AND c.colid = 1
  6538.             AND o.type IN ('TF', 'IF')
  6539.             AND '@TABLE_RETURN_VALUE' like @column_name
  6540.             AND c.number = 0
  6541.         ORDER BY 1, 2, 3, 18
  6542.     end
  6543. go
  6544.  
  6545. grant execute on sp_sproc_columns to public
  6546. go
  6547.  
  6548. dump tran master with no_log
  6549. go
  6550.  
  6551. print 'creating sp_statistics'
  6552. go
  6553.  
  6554. /*  Procedure for pre-7.0 server */
  6555. CREATE PROCEDURE sp_statistics (
  6556.                  @table_name        varchar(32),
  6557.                  @table_owner       varchar(32) = null,
  6558.                  @table_qualifier   varchar(32) = null,
  6559.                  @index_name        varchar(32) = '%',
  6560.                  @is_unique         char(1) = 'N',
  6561.                  @accuracy          char(1) = 'Q')
  6562. AS
  6563.     set nocount on
  6564.     DECLARE @indid              int
  6565.     DECLARE @lastindid          int
  6566.     DECLARE @table_id           int
  6567.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  6568.  
  6569.     create table #TmpIndex(
  6570.         TABLE_QUALIFIER varchar(32) NULL,
  6571.         TABLE_OWNER     varchar(32) NULL,
  6572.         TABLE_NAME      varchar(32) NOT NULL,
  6573.         INDEX_QUALIFIER varchar(32) null,
  6574.         INDEX_NAME      varchar(32) null,
  6575.         NON_UNIQUE      smallint null,
  6576.         TYPE            smallint NOT NULL,
  6577.         SEQ_IN_INDEX    smallint null,
  6578.         COLUMN_NAME     varchar(32) null,
  6579.         COLLATION       char(1) null,
  6580.         index_id        int null,
  6581.         CARDINALITY     int null,
  6582.         PAGES           int null,
  6583.         status          smallint NOT NULL)
  6584.  
  6585.     if @table_qualifier is not null
  6586.     begin
  6587.         if db_name() <> @table_qualifier
  6588.         begin   /* If qualifier doesn't match current database */
  6589.             raiserror 20001 '~~Rush_5~~'
  6590.             return
  6591.         end
  6592.     end
  6593.  
  6594.     if @accuracy not in ('Q','E')
  6595.         begin
  6596.             raiserror 20002 '~~Rush_58~~'
  6597.             return
  6598.         end
  6599.  
  6600.     if (@@trancount <> 0 and
  6601.         charindex('6.50', @@version) = 0 and
  6602.         charindex('7.00', @@version) = 0 and
  6603.         charindex('8.00', @@version) = 0)
  6604.     begin   /* If inside a transaction */
  6605.         raiserror 20003 '~~Rush_59~~'
  6606.         return
  6607.     end
  6608.  
  6609.     if @table_owner is null
  6610.     begin   /* If unqualified table name */
  6611.         SELECT @full_table_name = @table_name
  6612.     end
  6613.     else
  6614.     begin   /* Qualified table name */
  6615.         if @table_owner = ''
  6616.         begin   /* If empty owner name */
  6617.             SELECT @full_table_name = @table_owner
  6618.         end
  6619.         else
  6620.         begin
  6621.             SELECT @full_table_name = @table_owner + '.' + @table_name
  6622.         end
  6623.     end
  6624.     /*  Get Object ID */
  6625.     SELECT @table_id = object_id(@full_table_name)
  6626.  
  6627.     /*  Start at lowest index id */
  6628.     SELECT @indid = min(indid)
  6629.     FROM sysindexes
  6630.     WHERE id = @table_id
  6631.         AND indid > 0
  6632.         AND indid < 255
  6633.  
  6634.     WHILE @indid is not NULL
  6635.     BEGIN
  6636.         INSERT #TmpIndex    /* Add all columns that are in index */
  6637.             SELECT
  6638.                 DB_NAME(),                              /* TABLE_QUALIFIER */
  6639.                 USER_NAME(o.uid),                       /* TABLE_OWNER     */
  6640.                 o.name,                                 /* TABLE_NAME      */
  6641.                 o.name,                                 /* INDEX_QUALIFIER */
  6642.                 x.name,                                 /* INDEX_NAME      */
  6643.                 0,                                      /* NON_UNIQUE      */
  6644.                 1,                                      /* SQL_INDEX_CLUSTERED */
  6645.                 colid,                                  /* SEQ_IN_INDEX    */
  6646.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME     */
  6647.                 'A',                                    /* COLLATION       */
  6648.                 @indid,                                 /* index_id        */
  6649.                 x.rows,                                 /* CARDINALITY     */
  6650.                 x.dpages,                               /* PAGES           */
  6651.                 x.status                                /* status           */
  6652.             FROM sysindexes x, syscolumns c, sysobjects o
  6653.             WHERE
  6654.                 x.id = @table_id
  6655.                 AND x.id = o.id
  6656.                 AND x.id = c.id
  6657.                 AND c.colid < keycnt+(x.status&16)/16   /* all but Unique Clust indices have an extra key */
  6658.                 AND x.indid = @indid
  6659.         /*
  6660.         **    Now move @indid to the next index.
  6661.         */
  6662.         SELECT @lastindid = @indid
  6663.         SELECT @indid = NULL
  6664.  
  6665.         SELECT @indid = min(indid)
  6666.         FROM sysindexes
  6667.         WHERE id = @table_id
  6668.             AND indid > @lastindid
  6669.             AND indid < 255
  6670.     END
  6671.  
  6672.     UPDATE #TmpIndex
  6673.         SET NON_UNIQUE = 1
  6674.         WHERE status&2 <> 2 /* If non-unique index */
  6675.     UPDATE #TmpIndex
  6676.         SET
  6677.             TYPE = 3,           /* SQL_INDEX_OTHER */
  6678.             CARDINALITY = NULL,
  6679.             PAGES = NULL
  6680.         WHERE index_id > 1  /* If non-clustered index */
  6681.  
  6682.     /* now add row for table statistics */
  6683.     INSERT #TmpIndex
  6684.         SELECT
  6685.             DB_NAME(),              /* TABLE_QUALIFIER */
  6686.             USER_NAME(o.uid),       /* TABLE_OWNER     */
  6687.             o.name,                 /* TABLE_NAME      */
  6688.             null,                   /* INDEX_QUALIFIER */
  6689.             null,                   /* INDEX_NAME      */
  6690.             null,                   /* NON_UNIQUE      */
  6691.             0,                      /* SQL_TABLE_STAT  */
  6692.             null,                   /* SEQ_IN_INDEX    */
  6693.             null,                   /* COLUMN_NAME     */
  6694.             null,                   /* COLLATION       */
  6695.             0,                      /* index_id        */
  6696.             x.rows,                 /* CARDINALITY     */
  6697.             x.dpages,               /* PAGES           */
  6698.             0                       /* status          */
  6699.         FROM sysindexes x, sysobjects o
  6700.         WHERE o.id = @table_id
  6701.             AND x.id = o.id
  6702.             AND (x.indid = 0 or x.indid = 1)    /*  If there are no indexes */
  6703.                                                 /*  then table stats are in */
  6704.                                                 /*  a row with indid =0     */
  6705.  
  6706.     if @is_unique <> 'Y'    /* If all indexes desired */
  6707.         SELECT
  6708.             TABLE_QUALIFIER,
  6709.             TABLE_OWNER,
  6710.             TABLE_NAME,
  6711.             NON_UNIQUE,
  6712.             INDEX_QUALIFIER,
  6713.             INDEX_NAME,
  6714.             TYPE,
  6715.             SEQ_IN_INDEX,
  6716.             COLUMN_NAME,
  6717.             COLLATION,
  6718.             CARDINALITY,
  6719.             PAGES,
  6720.             FILTER_CONDITION = convert(varchar(128),null)
  6721.         FROM #TmpIndex
  6722.         WHERE
  6723.             INDEX_NAME like @index_name /* If matching name */
  6724.             or INDEX_NAME is null       /* If SQL_TABLE_STAT row */
  6725.         ORDER BY 4, 7, 6, 8
  6726.     else                    /* If only unique indexes desired */
  6727.         SELECT
  6728.             TABLE_QUALIFIER,
  6729.             TABLE_OWNER,
  6730.             TABLE_NAME,
  6731.             NON_UNIQUE,
  6732.             INDEX_QUALIFIER,
  6733.             INDEX_NAME,
  6734.             TYPE,
  6735.             SEQ_IN_INDEX,
  6736.             COLUMN_NAME,
  6737.             COLLATION,
  6738.             CARDINALITY,
  6739.             PAGES,
  6740.             FILTER_CONDITION = convert(varchar(128),null)
  6741.         FROM #TmpIndex
  6742.         WHERE
  6743.             (NON_UNIQUE = 0             /* If unique */
  6744.                 or NON_UNIQUE is NULL)  /* If SQL_TABLE_STAT row */
  6745.             and (INDEX_NAME like @index_name    /* If matching name */
  6746.                 or INDEX_NAME is null)  /* If SQL_TABLE_STAT row */
  6747.         ORDER BY 4, 7, 6, 8
  6748.  
  6749.     DROP TABLE #TmpIndex
  6750. go
  6751.  
  6752. if (charindex('7.00', @@version) = 0 and
  6753.     charindex('8.00', @@version) = 0)
  6754. begin
  6755.     print ''
  6756.     print ''
  6757.     print 'Warning:'
  6758.     print 'you are installing the stored procedures '
  6759.     print 'on a pre 7.0 SQL Server.'
  6760.     print 'Ignore the following errors.'
  6761. end
  6762. else
  6763.     drop proc sp_statistics
  6764. go
  6765.  
  6766. /*  Procedure for 7.0 server */
  6767. CREATE PROCEDURE sp_statistics (
  6768.                  @table_name        sysname,
  6769.                  @table_owner       sysname = null,
  6770.                  @table_qualifier   sysname = null,
  6771.                  @index_name        sysname = '%',
  6772.                  @is_unique         char(1) = 'N',
  6773.                  @accuracy          char(1) = 'Q')
  6774. AS
  6775.     set nocount on
  6776.     DECLARE @indid              int
  6777.     DECLARE @lastindid          int
  6778.     DECLARE @table_id           int
  6779.     DECLARE @full_table_name    nvarchar(257)
  6780.  
  6781.     create table #TmpIndex(
  6782.         TABLE_QUALIFIER sysname NULL,
  6783.         TABLE_OWNER     sysname NULL,
  6784.         TABLE_NAME      sysname NOT NULL,
  6785.         INDEX_QUALIFIER sysname null,
  6786.         INDEX_NAME      sysname null,
  6787.         NON_UNIQUE      smallint null,
  6788.         TYPE            smallint NOT NULL,
  6789.         SEQ_IN_INDEX    smallint null,
  6790.         COLUMN_NAME     sysname null,
  6791.         COLLATION       char(1) null,
  6792.         index_id        int null,
  6793.         CARDINALITY     int null,
  6794.         PAGES           int null,
  6795.         status          int NOT NULL)
  6796.  
  6797.     if @table_qualifier is not null
  6798.     begin
  6799.         if db_name() <> @table_qualifier
  6800.         begin   /* If qualifier doesn't match current database */
  6801.             raiserror (15250, -1,-1)
  6802.             return
  6803.         end
  6804.     end
  6805.  
  6806.     if @accuracy not in ('Q','E')
  6807.         begin
  6808.             raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
  6809.             return
  6810.         end
  6811.  
  6812.     if @table_owner is null
  6813.     begin   /* If unqualified table name */
  6814.         SELECT @full_table_name = quotename(@table_name)
  6815.     end
  6816.     else
  6817.     begin   /* Qualified table name */
  6818.         if @table_owner = ''
  6819.         begin   /* If empty owner name */
  6820.             SELECT @full_table_name = quotename(@table_owner)
  6821.         end
  6822.         else
  6823.         begin
  6824.             SELECT @full_table_name = quotename(@table_owner) +
  6825.                 '.' + quotename(@table_name)
  6826.         end
  6827.     end
  6828.     /*  Get Object ID */
  6829.     SELECT @table_id = object_id(@full_table_name)
  6830.  
  6831.     /*  Start at lowest index id */
  6832.     SELECT @indid = min(indid)
  6833.     FROM sysindexes
  6834.     WHERE not (@table_id is null)
  6835.         AND id = @table_id
  6836.         AND indid > 0
  6837.         AND indid < 255
  6838.  
  6839.     /* Create a temp table to correct the ordinal position of the columns */
  6840.     create table #TmpColumns
  6841.     (ordinal int identity(1,1),
  6842.      colid   smallint not null)
  6843.  
  6844.     /* Load columns into the temp table */
  6845.     insert into #TmpColumns (colid)
  6846.     select c.colid
  6847.     from syscolumns c
  6848.     where c.id = @table_id
  6849.     order by c.colid
  6850.     
  6851.     WHILE @indid is not NULL
  6852.     BEGIN
  6853.         INSERT #TmpIndex    /* Add all columns that are in index */
  6854.             SELECT
  6855.                 DB_NAME(),                              /* TABLE_QUALIFIER */
  6856.                 USER_NAME(o.uid),                       /* TABLE_OWNER     */
  6857.                 o.name,                                 /* TABLE_NAME      */
  6858.                 o.name,                                 /* INDEX_QUALIFIER */
  6859.                 x.name,                                 /* INDEX_NAME      */
  6860.                 case                                    /* NON_UNIQUE      */
  6861.                     WHEN x.status&2 <> 2 then 1         /* Nonunique index */
  6862.                     else 0                              /* Unique index    */
  6863.                 end,
  6864.                 case                                    /* TYPE            */
  6865.                     when @indid > 1 then 3              /* Non-Clustered   */
  6866.                     else 1                              /* Clustered index */
  6867.                 end,
  6868.                 tc.ordinal,                                 /* SEQ_IN_INDEX    */
  6869.                 INDEX_COL(@full_table_name, indid, tc.ordinal),/* COLUMN_NAME      */
  6870.                 'A',                                    /* COLLATION       */
  6871.                 @indid,                                 /* index_id        */
  6872.                 case                                    /* CARDINALITY     */
  6873.                     when @indid > 1 then NULL           /* Non-Clustered   */
  6874.                     else x.rows                         /* Clustered index */
  6875.                 end,
  6876.                 case                                    /* PAGES           */
  6877.                     when @indid > 1 then NULL           /* Non-Clustered   */
  6878.                     else x.dpages                       /* Clustered index */
  6879.                 end,
  6880.                 x.status                                /* status           */
  6881.             FROM sysindexes x, syscolumns c, sysobjects o, #TmpColumns tc
  6882.             WHERE
  6883.                 not (@table_id is null)
  6884.                 AND x.id = @table_id
  6885.                 AND x.id = o.id
  6886.                 AND x.id = c.id
  6887.                 AND tc.colid = c.colid
  6888.                 AND tc.ordinal < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  6889.                 AND INDEX_COL(@full_table_name, indid, tc.ordinal) IS NOT NULL
  6890.                 AND indid = @indid
  6891.                 AND (x.status&2 = 2
  6892.                     OR @is_unique <> 'Y')
  6893.                 AND (x.status&32) = 0
  6894.         /*
  6895.         **    Now move @indid to the next index.
  6896.         */
  6897.         SELECT @lastindid = @indid
  6898.         SELECT @indid = NULL
  6899.  
  6900.         SELECT @indid = min(indid)
  6901.         FROM sysindexes
  6902.         WHERE not (@table_id is null)
  6903.             AND id = @table_id
  6904.             AND indid > @lastindid
  6905.             AND indid < 255
  6906.     END
  6907.  
  6908.     /* now add row for table statistics */
  6909.     INSERT #TmpIndex
  6910.         SELECT
  6911.             DB_NAME(),              /* TABLE_QUALIFIER */
  6912.             USER_NAME(o.uid),       /* TABLE_OWNER     */
  6913.             o.name,                 /* TABLE_NAME      */
  6914.             null,                   /* INDEX_QUALIFIER */
  6915.             null,                   /* INDEX_NAME      */
  6916.             null,                   /* NON_UNIQUE      */
  6917.             0,                      /* SQL_TABLE_STAT  */
  6918.             null,                   /* SEQ_IN_INDEX    */
  6919.             null,                   /* COLUMN_NAME     */
  6920.             null,                   /* COLLATION       */
  6921.             0,                      /* index_id        */
  6922.             x.rows,                 /* CARDINALITY     */
  6923.             x.dpages,               /* PAGES           */
  6924.             0                       /* status          */
  6925.         FROM sysindexes x, sysobjects o
  6926.         WHERE not (@table_id is null)
  6927.             AND o.id = @table_id
  6928.             AND x.id = o.id
  6929.             AND (x.indid = 0 or x.indid = 1)    /*  If there are no indexes */
  6930.                                                 /*  then table stats are in */
  6931.                                                 /*  a row with indid =0     */
  6932.  
  6933.     SELECT
  6934.         TABLE_QUALIFIER,
  6935.         TABLE_OWNER,
  6936.         TABLE_NAME,
  6937.         NON_UNIQUE,
  6938.         INDEX_QUALIFIER,
  6939.         INDEX_NAME,
  6940.         TYPE,
  6941.         SEQ_IN_INDEX,
  6942.         COLUMN_NAME,
  6943.         COLLATION,
  6944.         CARDINALITY,
  6945.         PAGES,
  6946.         FILTER_CONDITION = convert(varchar(128),null)
  6947.     FROM #TmpIndex
  6948.     WHERE
  6949.         INDEX_NAME like @index_name /* If matching name */
  6950.         or INDEX_NAME is null       /* If SQL_TABLE_STAT row */
  6951.     ORDER BY 4, 7, 6, 8
  6952.  
  6953.     DROP TABLE #TmpIndex, #TmpColumns
  6954. go
  6955.  
  6956. if (charindex('8.00', @@version) = 0)
  6957. begin
  6958.     print ''
  6959.     print ''
  6960.     print 'Warning:'
  6961.     print 'you are installing the stored procedures '
  6962.     print 'on a pre 8.0 SQL Server.'
  6963.     print 'Ignore the following errors.'
  6964. end
  6965. else
  6966.     drop proc sp_statistics
  6967. go
  6968.  
  6969. /*  Procedure for 8.0 server */
  6970. CREATE PROCEDURE sp_statistics (
  6971.                  @table_name        sysname,
  6972.                  @table_owner       sysname = null,
  6973.                  @table_qualifier   sysname = null,
  6974.                  @index_name        sysname = '%',
  6975.                  @is_unique         char(1) = 'N',
  6976.                  @accuracy          char(1) = 'Q')
  6977. AS
  6978.     set nocount on
  6979.     DECLARE @indid              int
  6980.     DECLARE @lastindid          int
  6981.     DECLARE @table_id           int
  6982.     DECLARE @full_table_name    nvarchar(257)
  6983.  
  6984.     create table #TmpIndex(
  6985.         TABLE_QUALIFIER sysname collate database_default NULL,
  6986.         TABLE_OWNER     sysname collate database_default NULL,
  6987.         TABLE_NAME      sysname collate database_default NOT NULL,
  6988.         INDEX_QUALIFIER sysname collate database_default null,
  6989.         INDEX_NAME      sysname collate database_default null,
  6990.         NON_UNIQUE      smallint null,
  6991.         TYPE            smallint NOT NULL,
  6992.         SEQ_IN_INDEX    smallint null,
  6993.         COLUMN_NAME     sysname collate database_default null,
  6994.         COLLATION       char(1) collate database_default null,
  6995.         index_id        int null,
  6996.         CARDINALITY     int null,
  6997.         PAGES           int null,
  6998.         status          int NOT NULL)
  6999.  
  7000.     if @table_qualifier is not null
  7001.     begin
  7002.         if db_name() <> @table_qualifier
  7003.         begin   /* If qualifier doesn't match current database */
  7004.             raiserror (15250, -1,-1)
  7005.             return
  7006.         end
  7007.     end
  7008.  
  7009.     if @accuracy not in ('Q','E')
  7010.         begin
  7011.             raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
  7012.             return
  7013.         end
  7014.  
  7015.     if @table_owner is null
  7016.     begin   /* If unqualified table name */
  7017.         SELECT @full_table_name = quotename(@table_name)
  7018.     end
  7019.     else
  7020.     begin   /* Qualified table name */
  7021.         if @table_owner = ''
  7022.         begin   /* If empty owner name */
  7023.             SELECT @full_table_name = quotename(@table_owner)
  7024.         end
  7025.         else
  7026.         begin
  7027.             SELECT @full_table_name = quotename(@table_owner) +
  7028.                 '.' + quotename(@table_name)
  7029.         end
  7030.     end
  7031.     /*  Get Object ID */
  7032.     SELECT @table_id = object_id(@full_table_name)
  7033.  
  7034.     /*  Start at lowest index id */
  7035.     SELECT @indid = min(indid)
  7036.     FROM sysindexes
  7037.     WHERE not (@table_id is null)
  7038.         AND id = @table_id
  7039.         AND indid > 0
  7040.         AND indid < 255
  7041.  
  7042.     /* Create a temp table to correct the ordinal position of the columns */
  7043.     create table #TmpColumns
  7044.     (ordinal int identity(1,1),
  7045.      colid   smallint not null)
  7046.  
  7047.     /* Load columns into the temp table */
  7048.     insert into #TmpColumns (colid)
  7049.     select c.colid
  7050.     from syscolumns c
  7051.     where c.id = @table_id
  7052.     order by c.colid
  7053.     
  7054.     WHILE @indid is not NULL
  7055.     BEGIN
  7056.         INSERT #TmpIndex    /* Add all columns that are in index */
  7057.             SELECT
  7058.                 DB_NAME(),                              /* TABLE_QUALIFIER */
  7059.                 USER_NAME(o.uid),                       /* TABLE_OWNER     */
  7060.                 o.name,                                 /* TABLE_NAME      */
  7061.                 o.name,                                 /* INDEX_QUALIFIER */
  7062.                 x.name,                                 /* INDEX_NAME      */
  7063.                 case                                    /* NON_UNIQUE      */
  7064.                     WHEN x.status&2 <> 2 then 1         /* Nonunique index */
  7065.                     else 0                              /* Unique index    */
  7066.                 end,
  7067.                 case                                    /* TYPE            */
  7068.                     when @indid > 1 then 3              /* Non-Clustered   */
  7069.                     else 1                              /* Clustered index */
  7070.                 end,
  7071.                 tc.ordinal,                                 /* SEQ_IN_INDEX    */
  7072.                 INDEX_COL(@full_table_name, indid, tc.ordinal),/* COLUMN_NAME      */
  7073.                 'A',                                    /* COLLATION       */
  7074.                 @indid,                                 /* index_id        */
  7075.                 case                                    /* CARDINALITY     */
  7076.                     when @indid > 1 then NULL           /* Non-Clustered   */
  7077.                     else x.rows                         /* Clustered index */
  7078.                 end,
  7079.                 case                                    /* PAGES           */
  7080.                     when @indid > 1 then NULL           /* Non-Clustered   */
  7081.                     else x.dpages                       /* Clustered index */
  7082.                 end,
  7083.                 x.status                                /* status           */
  7084.             FROM sysindexes x, syscolumns c, sysobjects o, #TmpColumns tc
  7085.             WHERE
  7086.                 not (@table_id is null)
  7087.                 AND x.id = @table_id
  7088.                 AND x.id = o.id
  7089.                 AND x.id = c.id
  7090.                 AND tc.colid = c.colid
  7091.                 AND tc.ordinal < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  7092.                 AND INDEX_COL(@full_table_name, indid, tc.ordinal) IS NOT NULL
  7093.                 AND indid = @indid
  7094.                 AND (x.status&2 = 2
  7095.                     OR @is_unique <> 'Y')
  7096.                 AND (x.status&32) = 0
  7097.         /*
  7098.         **    Now move @indid to the next index.
  7099.         */
  7100.         SELECT @lastindid = @indid
  7101.         SELECT @indid = NULL
  7102.  
  7103.         SELECT @indid = min(indid)
  7104.         FROM sysindexes
  7105.         WHERE not (@table_id is null)
  7106.             AND id = @table_id
  7107.             AND indid > @lastindid
  7108.             AND indid < 255
  7109.     END
  7110.  
  7111.     /* now add row for table statistics */
  7112.     INSERT #TmpIndex
  7113.         SELECT
  7114.             DB_NAME(),              /* TABLE_QUALIFIER */
  7115.             USER_NAME(o.uid),       /* TABLE_OWNER     */
  7116.             o.name,                 /* TABLE_NAME      */
  7117.             null,                   /* INDEX_QUALIFIER */
  7118.             null,                   /* INDEX_NAME      */
  7119.             null,                   /* NON_UNIQUE      */
  7120.             0,                      /* SQL_TABLE_STAT  */
  7121.             null,                   /* SEQ_IN_INDEX    */
  7122.             null,                   /* COLUMN_NAME     */
  7123.             null,                   /* COLLATION       */
  7124.             0,                      /* index_id        */
  7125.             x.rows,                 /* CARDINALITY     */
  7126.             x.dpages,               /* PAGES           */
  7127.             0                       /* status          */
  7128.         FROM sysindexes x, sysobjects o
  7129.         WHERE not (@table_id is null)
  7130.             AND o.id = @table_id
  7131.             AND x.id = o.id
  7132.             AND (x.indid = 0 or x.indid = 1)    /*  If there are no indexes */
  7133.                                                 /*  then table stats are in */
  7134.                                                 /*  a row with indid =0     */
  7135.  
  7136.     SELECT
  7137.         TABLE_QUALIFIER,
  7138.         TABLE_OWNER,
  7139.         TABLE_NAME,
  7140.         NON_UNIQUE,
  7141.         INDEX_QUALIFIER,
  7142.         INDEX_NAME,
  7143.         TYPE,
  7144.         SEQ_IN_INDEX,
  7145.         COLUMN_NAME,
  7146.         COLLATION,
  7147.         CARDINALITY,
  7148.         PAGES,
  7149.         FILTER_CONDITION = convert(varchar(128),null)
  7150.     FROM #TmpIndex
  7151.     WHERE
  7152.         INDEX_NAME like @index_name /* If matching name */
  7153.         or INDEX_NAME is null       /* If SQL_TABLE_STAT row */
  7154.     ORDER BY 4, 7, 6, 8
  7155.  
  7156.     DROP TABLE #TmpIndex, #TmpColumns
  7157. go
  7158.  
  7159. grant execute on sp_statistics to public
  7160. go
  7161.  
  7162. dump tran master with no_log
  7163. go
  7164.  
  7165. print 'creating sp_stored_procedures'
  7166. go
  7167.  
  7168.  
  7169. /* pre 8.0 version */
  7170. create procedure sp_stored_procedures(
  7171.                         @sp_name        varchar(102) = null,
  7172.                         @sp_owner       varchar(96) = null,
  7173.                         @sp_qualifier   varchar(32) = null)
  7174. as
  7175.     declare @proc_type smallint
  7176.  
  7177.     if @sp_qualifier is not null
  7178.     begin
  7179.         if db_name() <> @sp_qualifier
  7180.         begin
  7181.             if @sp_qualifier = ''
  7182.             begin
  7183.                 /* in this case, we need to return an empty result set */
  7184.                 /* because the user has requested a database with an empty name */
  7185.                 select @sp_name = ''
  7186.                 select @sp_owner = ''
  7187.             end else
  7188.             begin   /* If qualifier doesn't match current database */
  7189.                 raiserror 20001 '~~Rush_51~~'
  7190.                 return
  7191.             end
  7192.         end
  7193.     end
  7194.  
  7195.     if @sp_name is null
  7196.     begin  /*  If procedure name not supplied, match all */
  7197.         select @sp_name = '%'
  7198.     end
  7199.     else begin
  7200.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  7201.         begin
  7202.             if exists (select * from sysobjects
  7203.                 where uid = user_id()
  7204.                     and name = @sp_name
  7205.                     and type = 'P') /* Object type of Procedure */
  7206.             begin
  7207.                 select @sp_owner = user_name()
  7208.             end
  7209.         end
  7210.     end
  7211.     if @sp_owner is null    /*  If procedure owner not supplied, match all */
  7212.         select @sp_owner = '%'
  7213.  
  7214.     select @proc_type=2     /* Return 2 for 4.2 and later servers. */
  7215.  
  7216.     select
  7217.         PROCEDURE_QUALIFIER = convert(varchar(32),db_name()),
  7218.         PROCEDURE_OWNER = convert(varchar(32),user_name(o.uid)),
  7219.         PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  7220.         NUM_INPUT_PARAMS = -1,  /* Constant since value unknown */
  7221.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  7222.         NUM_RESULT_SETS = -1,   /* Constant since value unknown */
  7223.         REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  7224.         PROCEDURE_TYPE = @proc_type
  7225.     from
  7226.         sysobjects o,syscomments c,sysusers u
  7227.     where
  7228.         o.name like @sp_name
  7229.         and c.colid = 1
  7230.         and user_name(o.uid) like @sp_owner
  7231.         and o.type = 'P'        /* Object type of Procedure */
  7232.         and c.id = o.id
  7233.         and u.uid = user_id()   /* constrain sysusers uid for use in subquery */
  7234.         and (suser_id() = 1     /* User is the System Administrator */
  7235.             or o.uid = user_id()    /* User created the object */
  7236.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  7237.             or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  7238.              from sysprotects p
  7239.              /* outer join to correlate with all rows in sysobjects */
  7240.              where p.id =* o.id
  7241.                  /*  get rows for public,current user,user's group */
  7242.                  and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  7243.                  /* check for SELECT,EXECUTE privilege */
  7244.                  and (action in (193,224)))&1    /* more magic...normalize GRANT */
  7245.             ) = 1    /* final magic...compare Grants    */
  7246.         )
  7247.     order by 1, 2, 3
  7248. go
  7249.  
  7250. grant execute on sp_stored_procedures to public
  7251. go
  7252.  
  7253.  
  7254. if (charindex('7.00', @@version) > 0 or
  7255.     charindex('8.00', @@version) > 0)
  7256.     drop procedure sp_stored_procedures
  7257. else
  7258. begin
  7259.     print ''
  7260.     print ''
  7261.     print 'Warning:'
  7262.     print 'you are installing the stored procedures '
  7263.     print 'on a pre 7.0 SQL Server.'
  7264.     print 'Ignore the following errors.'
  7265. end
  7266. go
  7267.  
  7268.  
  7269. /* 7.0 version */
  7270. create procedure sp_stored_procedures(
  7271.                         @sp_name        nvarchar(390) = null,
  7272.                         @sp_owner       nvarchar(384) = null,
  7273.                         @sp_qualifier   sysname = null)
  7274. as
  7275.     declare @proc_type smallint
  7276.  
  7277.     if @sp_qualifier is not null
  7278.     begin
  7279.         if db_name() <> @sp_qualifier
  7280.         begin
  7281.             if @sp_qualifier = ''
  7282.             begin
  7283.                 /* in this case, we need to return an empty result set */
  7284.                 /* because the user has requested a database with an empty name */
  7285.                 select @sp_name = ''
  7286.                 select @sp_owner = ''
  7287.             end else
  7288.             begin   /* If qualifier doesn't match current database */
  7289.                 raiserror (15250, -1,-1)
  7290.                 return
  7291.             end
  7292.         end
  7293.     end
  7294.  
  7295.     if @sp_name is null
  7296.     begin  /*  If procedure name not supplied, match all */
  7297.         select @sp_name = '%'
  7298.     end
  7299.     else begin
  7300.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  7301.         begin
  7302.             if exists (select * from sysobjects
  7303.                 where uid = user_id()
  7304.                     and name = @sp_name
  7305.                     and type = 'P') /* Object type of Procedure */
  7306.             begin
  7307.                 select @sp_owner = user_name()
  7308.             end
  7309.         end
  7310.     end
  7311.     if @sp_owner is null    /*  If procedure owner not supplied, match all */
  7312.         select @sp_owner = '%'
  7313.  
  7314.     select @proc_type=2     /* Return 2 for 4.2 and later servers. */
  7315.  
  7316.     select
  7317.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  7318.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  7319.         PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  7320.         NUM_INPUT_PARAMS = -1,  /* Constant since value unknown */
  7321.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  7322.         NUM_RESULT_SETS = -1,   /* Constant since value unknown */
  7323.         REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  7324.         PROCEDURE_TYPE = @proc_type
  7325.     from
  7326.         sysobjects o,syscomments c
  7327.     where
  7328.         o.name like @sp_name
  7329.         and c.colid = 1
  7330.         and user_name(o.uid) like @sp_owner
  7331.         and o.type = 'P'        /* Object type of Procedure */
  7332.         and c.id = o.id
  7333.         and permissions (o.id)&32 <> 0
  7334.     order by 1, 2, 3
  7335. go
  7336.  
  7337. grant execute on sp_stored_procedures to public
  7338. go
  7339.  
  7340. if (charindex('8.00', @@version) > 0)
  7341.     drop procedure sp_stored_procedures
  7342. else
  7343. begin
  7344.     print ''
  7345.     print ''
  7346.     print 'Warning:'
  7347.     print 'you are installing the stored procedures '
  7348.     print 'on a pre 8.0 SQL Server.'
  7349.     print 'Ignore the following errors.'
  7350. end
  7351. go
  7352.  
  7353.  
  7354. /* 8.0 version */
  7355. create procedure sp_stored_procedures(
  7356.                         @sp_name    nvarchar(390) = null,
  7357.                         @sp_owner   nvarchar(384) = null,
  7358.                         @sp_qualifier   sysname = null)
  7359. as
  7360.     declare @proc_type smallint
  7361.  
  7362.     if @sp_qualifier is not null
  7363.     begin
  7364.         if db_name() <> @sp_qualifier
  7365.         begin
  7366.             if @sp_qualifier = ''
  7367.             begin
  7368.                 /* in this case, we need to return an empty result set */
  7369.                 /* because the user has requested a database with an empty name */
  7370.                 select @sp_name = ''
  7371.                 select @sp_owner = ''
  7372.             end else
  7373.             begin   /* If qualifier doesn't match current database */
  7374.                 raiserror (15250, -1,-1)
  7375.                 return
  7376.             end
  7377.         end
  7378.     end
  7379.  
  7380.     if @sp_name is null
  7381.     begin  /*  If procedure name not supplied, match all */
  7382.         select @sp_name = '%'
  7383.     end
  7384.     else
  7385.     begin
  7386.     if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  7387.     begin
  7388.         if exists (select * from sysobjects
  7389.             where uid = user_id()
  7390.                 and name = @sp_name
  7391.                 and type in ('P', 'FN', 'TF', 'IF'))        /* Procedures, scalar UDF, table UDF */
  7392.         begin
  7393.             select @sp_owner = user_name()
  7394.         end
  7395.     end
  7396.     end
  7397.     
  7398.     if @sp_owner is null        /* If procedure owner not supplied, match all */
  7399.     select @sp_owner = '%'
  7400.  
  7401.     select @proc_type=2         /* Return 2 for 4.2 and later servers. */
  7402.  
  7403.     select
  7404.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  7405.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  7406.         PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  7407.         NUM_INPUT_PARAMS = -1,  /* Constant since value unknown */
  7408.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  7409.         NUM_RESULT_SETS = -1,   /* Constant since value unknown */
  7410.         REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  7411.         PROCEDURE_TYPE = @proc_type
  7412.     from
  7413.         sysobjects o,syscomments c
  7414.     where
  7415.         o.name like @sp_name
  7416.         and c.colid = 1
  7417.         and user_name(o.uid) like @sp_owner
  7418.         and o.type in ('P', 'FN', 'TF', 'IF')       /* Object type of Procedure, scalar UDF, table UDF */
  7419.         and c.id = o.id
  7420.         and permissions (o.id)&32 <> 0
  7421.     order by 1, 2, 3
  7422. go
  7423.  
  7424. grant execute on sp_stored_procedures to public
  7425. go
  7426.  
  7427. dump tran master with no_log
  7428. go
  7429.  
  7430.  
  7431. print 'creating sp_table_privileges'
  7432. go
  7433.  
  7434. /*  Procedure for pre 6.50 server */
  7435. CREATE PROCEDURE sp_table_privileges (
  7436.             @table_name         varchar(90),
  7437.             @table_owner        varchar(90) = null,
  7438.             @table_qualifier    varchar(32) = null)
  7439. as
  7440.     set nocount on
  7441.  
  7442.     declare @table_id    int,
  7443.             @owner_id    int,
  7444.             @full_table_name char(181)
  7445.     declare @refconst int
  7446.  
  7447.     select @refconst = 1
  7448.     if  (charindex('6.00', @@version) = 0)
  7449.         select @refconst = NULL
  7450.  
  7451.     if @table_qualifier is not null
  7452.     begin
  7453.         if db_name() != @table_qualifier
  7454.         begin   /* If qualifier doesn't match current database */
  7455.             raiserror 20001 'Table qualifier must be name of current database'
  7456.             return
  7457.         end
  7458.     end
  7459.     if @table_owner is null
  7460.     begin   /* If unqualified table name */
  7461.         SELECT @full_table_name = @table_name
  7462.     end
  7463.     else
  7464.     begin   /* Qualified table name */
  7465.         SELECT @full_table_name = @table_owner + '.' + @table_name
  7466.     end
  7467.     /*  Get Object ID */
  7468.     SELECT @table_id = object_id(@full_table_name)
  7469.  
  7470.     if @@trancount != 0
  7471.     begin   /* If inside a transaction */
  7472.         raiserror 20003 'The procedure ''sp_table_privileges'' cannot be executed from within a transaction.'
  7473.         return
  7474.     end
  7475.     create table #table_priv1(
  7476.         table_qualifier         varchar(32) NOT NULL,
  7477.         table_owner             varchar(32) NOT NULL,
  7478.         table_name              varchar(32) NOT NULL,
  7479.         grantor                 varchar(32) NOT NULL,
  7480.         grantee                 varchar(32) NOT NULL,
  7481.         select_privilege        int NOT NULL,
  7482.         insert_privilege        int NOT NULL,
  7483.         update_privilege        int NOT NULL,
  7484.         delete_privilege        int NOT NULL,
  7485.         references_privilege    int NULL,
  7486.         is_grantable            varchar(3) NOT NULL,
  7487.         uid                     int NOT NULL,
  7488.         gid                     int NOT NULL)
  7489.  
  7490.     insert into #table_priv1
  7491.         select distinct
  7492.             db_name(),
  7493.             user_name(o.uid),
  7494.             o.name,
  7495.             user_name(o.uid),
  7496.             u.name,
  7497.             0,
  7498.             0,
  7499.             0,
  7500.             0,
  7501.             @refconst,
  7502.             'no',
  7503.             u.uid,
  7504.             u.gid
  7505.         from sysusers u, sysobjects o
  7506.         where o.id = @table_id and u.uid != u.gid
  7507.            and sysstat & 0xf in (1,2,3) /* only valid for system tables,
  7508.                                         ** user tables, and views. */
  7509.  
  7510.     /*
  7511.     ** now add row for table owner
  7512.     */
  7513.     if exists (
  7514.         select *
  7515.             from #table_priv1
  7516.             where grantor = grantee)
  7517.     begin
  7518.         update #table_priv1
  7519.         set
  7520.             select_privilege = 1,
  7521.             update_privilege = 1,
  7522.             insert_privilege = 1,
  7523.             delete_privilege = 1,
  7524.             references_privilege = 1,
  7525.             is_grantable = 'yes'
  7526.         where grantor = grantee
  7527.     end
  7528.     else
  7529.     begin
  7530.         insert into #table_priv1
  7531.             select  db_name(),
  7532.                 user_name(o.uid),
  7533.                 o.name,
  7534.                 user_name(o.uid),
  7535.                 user_name(o.uid),
  7536.                 1,
  7537.                 1,
  7538.                 1,
  7539.                 1,
  7540.                 @refconst,
  7541.                 'yes',
  7542.                 o.uid,
  7543.                 u.gid
  7544.             from sysobjects o, sysusers u
  7545.             where o.id = @table_id and u.uid = o.uid
  7546.             and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  7547.                                         ** user tables, and views. */
  7548.  
  7549.     end
  7550.  
  7551.     update #table_priv1
  7552.     set select_privilege = 1
  7553.     where
  7554.         exists (
  7555.             select * from sysprotects
  7556.             where
  7557.                 id = @table_id
  7558.                 and (#table_priv1.uid = uid
  7559.                     or #table_priv1.gid = uid
  7560.                     or uid = 0)
  7561.                 and protecttype = 205
  7562.                 and action = 193)
  7563.         and not exists (
  7564.             select * from sysprotects
  7565.             where
  7566.                 id = @table_id
  7567.                 and (#table_priv1.uid = uid
  7568.                     or #table_priv1.gid = uid
  7569.                     or uid = 0)
  7570.                 and protecttype = 206
  7571.                 and action = 193)
  7572.  
  7573.     update #table_priv1
  7574.     set insert_privilege = 1
  7575.     where
  7576.         exists (
  7577.             select * from sysprotects
  7578.             where
  7579.                 id = @table_id
  7580.                 and (#table_priv1.uid = uid
  7581.                     or #table_priv1.gid = uid
  7582.                     or uid = 0)
  7583.                 and protecttype = 205
  7584.                 and action = 195)
  7585.         and not exists (
  7586.             select * from sysprotects
  7587.             where
  7588.                 id = @table_id
  7589.                 and (#table_priv1.uid = uid
  7590.                     or #table_priv1.gid = uid
  7591.                     or uid = 0)
  7592.                 and protecttype = 206
  7593.                 and action = 195)
  7594.  
  7595.     update #table_priv1
  7596.     set delete_privilege = 1
  7597.     where
  7598.         exists (
  7599.             select * from sysprotects
  7600.             where
  7601.                 id = @table_id
  7602.                 and (#table_priv1.uid = uid
  7603.                     or #table_priv1.gid = uid
  7604.                     or uid = 0)
  7605.                 and protecttype = 205
  7606.                 and action = 196)
  7607.         and not exists (select * from sysprotects
  7608.             where
  7609.                 id = @table_id
  7610.                 and (#table_priv1.uid = uid
  7611.                     or #table_priv1.gid = uid
  7612.                     or uid = 0)
  7613.                 and protecttype = 206
  7614.                 and action = 196)
  7615.  
  7616.     update #table_priv1
  7617.     set update_privilege = 1
  7618.     where
  7619.         exists (
  7620.             select * from sysprotects
  7621.             where
  7622.                 id = @table_id
  7623.                 and (#table_priv1.uid = uid
  7624.                     or #table_priv1.gid = uid
  7625.                     or uid = 0)
  7626.                 and protecttype = 205
  7627.                 and action = 197)
  7628.         and not exists (
  7629.             select * from sysprotects
  7630.             where
  7631.                 id = @table_id
  7632.                 and (#table_priv1.uid = uid
  7633.                     or #table_priv1.gid = uid
  7634.                     or uid = 0)
  7635.                 and protecttype = 206
  7636.                 and action = 197)
  7637.  
  7638.     update #table_priv1
  7639.     set references_privilege = 1
  7640.     where
  7641.         exists (
  7642.             select * from sysprotects
  7643.             where
  7644.                 id = @table_id
  7645.                 and (#table_priv1.uid = uid
  7646.                     or #table_priv1.gid = uid
  7647.                     or uid = 0)
  7648.                 and protecttype = 205
  7649.                 and action = 26)
  7650.         and not exists (
  7651.             select * from sysprotects
  7652.             where
  7653.                 id = @table_id
  7654.                 and (#table_priv1.uid = uid
  7655.                     or #table_priv1.gid = uid
  7656.                     or uid = 0)
  7657.                 and protecttype = 206
  7658.                 and action = 26)
  7659.  
  7660.     create table #table_priv2(
  7661.         table_qualifier varchar(32) NULL,
  7662.         table_owner     varchar(32) NULL,
  7663.         table_name      varchar(32) NOT NULL,
  7664.         grantor         varchar(32) NULL,
  7665.         grantee         varchar(32) NOT NULL,
  7666.         privilege       varchar(32) NOT NULL,
  7667.         is_grantable    varchar(3) NULL)
  7668.  
  7669.     insert into #table_priv2
  7670.         select
  7671.             table_qualifier,
  7672.             table_owner,
  7673.             table_name,
  7674.             grantor,
  7675.             grantee,
  7676.             'SELECT',
  7677.             is_grantable
  7678.         from #table_priv1
  7679.         where select_privilege = 1
  7680.  
  7681.  
  7682.     insert into #table_priv2
  7683.         select
  7684.             table_qualifier,
  7685.             table_owner,
  7686.             table_name,
  7687.             grantor,
  7688.             grantee,
  7689.             'INSERT',
  7690.             is_grantable
  7691.         from #table_priv1
  7692.         where insert_privilege = 1
  7693.  
  7694.  
  7695.     insert into #table_priv2
  7696.         select
  7697.             table_qualifier,
  7698.             table_owner,
  7699.             table_name,
  7700.             grantor,
  7701.             grantee,
  7702.             'DELETE',
  7703.             is_grantable
  7704.         from #table_priv1
  7705.         where delete_privilege = 1
  7706.  
  7707.  
  7708.     insert into #table_priv2
  7709.         select
  7710.             table_qualifier,
  7711.             table_owner,
  7712.             table_name,
  7713.             grantor,
  7714.             grantee,
  7715.             'UPDATE',
  7716.             is_grantable
  7717.         from #table_priv1
  7718.         where update_privilege = 1
  7719.  
  7720.     insert into #table_priv2
  7721.         select
  7722.             table_qualifier,
  7723.             table_owner,
  7724.             table_name,
  7725.             grantor,
  7726.             grantee,
  7727.             'REFERENCES',
  7728.             is_grantable
  7729.         from #table_priv1
  7730.         where references_privilege = 1
  7731.  
  7732.  
  7733.     select * from #table_priv2
  7734.     order by table_owner,table_name,privilege,grantee
  7735. /*  order by 2,3,6,5 Can't use since fails on 4.21a server */
  7736. go
  7737.  
  7738. if (charindex('6.50', @@version) = 0 and
  7739.     charindex('7.00', @@version) = 0 and
  7740.     charindex('8.00', @@version) = 0)
  7741. begin
  7742.     print ''
  7743.     print ''
  7744.     print 'Warning:'
  7745.     print 'you are installing the stored procedures '
  7746.     print 'on a pre 6.50 SQL Server.'
  7747.     print 'Ignore the following errors.'
  7748. end
  7749. else
  7750.     drop proc sp_table_privileges
  7751. go
  7752.  
  7753.  
  7754. /*  Procedure for 6.50 server */
  7755. CREATE PROCEDURE sp_table_privileges (
  7756.             @table_name         varchar(96),
  7757.             @table_owner        varchar(96) = null,
  7758.             @table_qualifier    varchar(32) = null)
  7759. as
  7760.  
  7761.     if @table_qualifier is not null
  7762.     begin
  7763.         if db_name() <> @table_qualifier
  7764.         begin   /* If qualifier doesn't match current database */
  7765.             raiserror (15250, -1,-1)
  7766.             return
  7767.         end
  7768.     end
  7769.     if @table_name is null
  7770.         select @table_name = '%'
  7771.     if @table_owner is null /* If no owner supplied, force wildcard */
  7772.         select @table_owner = '%'
  7773.  
  7774.     select
  7775.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  7776.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  7777.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  7778.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  7779.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  7780.         convert(varchar(32),case p.action
  7781.              when 193 then 'SELECT'
  7782.              when 195 then 'INSERT'
  7783.              when 196 then 'DELETE'
  7784.              when 197 then 'UPDATE'
  7785.              else 'REFERENCES'
  7786.         end) PRIVILEGE,
  7787.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  7788.             else 'YES'
  7789.         end) IS_GRANTABLE
  7790.     from sysprotects p, sysobjects o, sysusers u
  7791.     where
  7792.         p.id = o.id
  7793.         and o.type in ('U','V','S')
  7794.         and object_name(o.id) like @table_name
  7795.         and user_name(o.uid) like @table_owner
  7796.             /* expand groups */
  7797.         and ((p.uid = u.uid and u.uid <> u.gid) or
  7798.              (p.uid = u.gid and u.uid <> u.gid))
  7799.         and p.protecttype <> 206    /* only grant rows */
  7800.         and p.action in (26,193,195,196,197)
  7801.         and o.uid <> u.uid          /* no rows for owner */
  7802.         and not exists (            /* exclude revoke'd privileges */
  7803.             select *
  7804.             from sysprotects p1
  7805.             where
  7806.                 p1.protecttype = 206
  7807.                 and p1.action = p.action
  7808.                 and p1.id = p.id
  7809.                 and p1.uid = u.uid)
  7810.     union
  7811.     select  /*  Add rows for table owner */
  7812.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  7813.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  7814.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  7815.         convert(varchar(32),user_name(u.uid)) GRANTOR,
  7816.         convert(varchar(32),user_name(o.uid)) GRANTEE,
  7817.         convert(varchar(32),case v.number
  7818.             when 193 then 'SELECT'
  7819.             when 195 then 'INSERT'
  7820.             when 196 then 'DELETE'
  7821.             when 197 then 'UPDATE'
  7822.             else 'REFERENCES'
  7823.         end) PRIVILEGE,
  7824.         convert(varchar(3),'YES') IS_GRANTABLE
  7825.     from sysobjects o, master.dbo.spt_values v, sysusers u
  7826.     where
  7827.         object_name(o.id) like @table_name
  7828.         and o.type in ('U','V','S')
  7829.         and user_name(o.uid) like @table_owner
  7830.         and u.uid = 1       /* grantor is dbo of database */
  7831.         and v.type = 'P'    /* cross product to get all exposed privileges */
  7832.         and v.number in (26,193,195,196,197)
  7833.         and not exists (    /* exclude revoke'd privileges */
  7834.             select *
  7835.             from sysprotects p1
  7836.             where
  7837.                 p1.protecttype = 206
  7838.                 and p1.action = v.number
  7839.                 and p1.id = o.id
  7840.                 and p1.uid = o.uid)
  7841.     order by 2,3,6,5
  7842. go
  7843.  
  7844.  
  7845. if (charindex('7.00', @@version) > 0 or
  7846.     charindex('8.00', @@version) > 0)
  7847.     drop proc sp_table_privileges
  7848. else
  7849. begin
  7850.     print ''
  7851.     print ''
  7852.     print 'Warning:'
  7853.     print 'you are installing the stored procedures '
  7854.     print 'on a pre 7.0 SQL Server.'
  7855.     print 'Ignore the following errors.'
  7856. end
  7857. go
  7858.  
  7859. /*  Procedure for 7.0 and later servers */
  7860. CREATE PROCEDURE sp_table_privileges (
  7861.             @table_name         nvarchar(384),
  7862.             @table_owner        nvarchar(384) = null,
  7863.             @table_qualifier    sysname = null)
  7864. as
  7865.  
  7866.     if @table_qualifier is not null
  7867.     begin
  7868.         if db_name() <> @table_qualifier
  7869.         begin   /* If qualifier doesn't match current database */
  7870.             raiserror (15250, -1,-1)
  7871.             return
  7872.         end
  7873.     end
  7874.     if @table_name is null
  7875.         select @table_name = '%'
  7876.     if @table_owner is null /* If no owner supplied, force wildcard */
  7877.         select @table_owner = '%'
  7878.  
  7879.     select
  7880.         convert(sysname,db_name()) TABLE_QUALIFIER,
  7881.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  7882.         convert(sysname,object_name(o.id)) TABLE_NAME,
  7883.         convert(sysname,user_name(p.grantor)) GRANTOR,
  7884.         convert(sysname,user_name(u.uid)) GRANTEE,
  7885.         convert(varchar(32),case p.action
  7886.              when 193 then 'SELECT'
  7887.              when 195 then 'INSERT'
  7888.              when 196 then 'DELETE'
  7889.              when 197 then 'UPDATE'
  7890.              else 'REFERENCES'
  7891.         end) PRIVILEGE,
  7892.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  7893.             else 'YES'
  7894.         end) IS_GRANTABLE
  7895.     from sysprotects p, sysobjects o, sysusers u, sysmembers m
  7896.     where
  7897.         p.id = o.id
  7898.         and o.type in ('U','V','S')
  7899.         and object_name(o.id) like @table_name
  7900.         and user_name(o.uid) like @table_owner
  7901.             /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  7902.         and (u.uid > 0 and u.uid < 16384)
  7903.         and ((p.uid = u.uid) or
  7904.              (p.uid = m.groupuid and u.uid = m.memberuid))
  7905.         and p.protecttype <> 206    /* only grant rows */
  7906.         and p.action in (26,193,195,196,197)
  7907.         and o.uid <> u.uid          /* no rows for owner */
  7908.         and not exists (            /* exclude revoke'd privileges */
  7909.             select *
  7910.             from sysprotects p1
  7911.             where
  7912.                 p1.protecttype = 206
  7913.                 and p1.action = p.action
  7914.                 and p1.id = p.id
  7915.                 and p1.uid = u.uid)
  7916.     union
  7917.     select  /*  Add rows for table owner */
  7918.         convert(sysname,db_name()) TABLE_QUALIFIER,
  7919.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  7920.         convert(sysname,object_name(o.id)) TABLE_NAME,
  7921.         convert(sysname,user_name(u.uid)) GRANTOR,
  7922.         convert(sysname,user_name(o.uid)) GRANTEE,
  7923.         convert(varchar(32),case v.number
  7924.             when 193 then 'SELECT'
  7925.             when 195 then 'INSERT'
  7926.             when 196 then 'DELETE'
  7927.             when 197 then 'UPDATE'
  7928.             else 'REFERENCES'
  7929.         end) PRIVILEGE,
  7930.         convert(varchar(3),'YES') IS_GRANTABLE
  7931.     from sysobjects o, master.dbo.spt_values v, sysusers u
  7932.     where
  7933.         object_name(o.id) like @table_name
  7934.         and o.type in ('U','V','S')
  7935.         and user_name(o.uid) like @table_owner
  7936.         and u.uid = 1       /* grantor is 'dbo' of database */
  7937.         and v.type = N'P'   /* cross product to get all exposed privileges */
  7938.         and v.number in (26,193,195,196,197)
  7939.         and not exists (    /* exclude revoke'd privileges */
  7940.             select *
  7941.             from sysprotects p1
  7942.             where
  7943.                 p1.protecttype = 206
  7944.                 and p1.action = v.number
  7945.                 and p1.id = o.id
  7946.                 and p1.uid = o.uid)
  7947.     order by 2,3,6,5
  7948. go
  7949.  
  7950. grant execute on sp_table_privileges to public
  7951. go
  7952.  
  7953. dump tran master with no_log
  7954. go
  7955.  
  7956. print 'creating sp_tables'
  7957. go
  7958.  
  7959. /*  Procedure for 6.50 and earlier servers */
  7960. create procedure sp_tables(
  7961.                @table_name      varchar(96) = null,
  7962.                @table_owner     varchar(96) = null,
  7963.                @table_qualifier varchar(32) = null,
  7964.                @table_type      varchar(100) = null)
  7965. as
  7966.     declare @type1 varchar(3)
  7967.     declare @tableindex int
  7968.  
  7969.  
  7970.     /* Special feature #1:  enumerate databases when owner and name
  7971.          are blank but qualifier is explicitly '%'.  */
  7972.     if @table_qualifier = '%' and
  7973.         @table_owner = '' and
  7974.         @table_name = ''
  7975.     begin   /* If enumerating databases */
  7976.         select
  7977.             TABLE_QUALIFIER = convert(varchar(32),d.name),
  7978.             TABLE_OWNER = convert(varchar(32),null),
  7979.             TABLE_NAME = convert(varchar(32),null),
  7980.             TABLE_TYPE = convert(varchar(32),null),
  7981.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  7982.         from master.dbo.sysdatabases d
  7983.         where d.name <> 'model' /* eliminate MODEL database */
  7984.         order by 1
  7985.     end
  7986.  
  7987.     /* Special feature #2:  enumerate owners when qualifier and name
  7988.          are blank but owner is explicitly '%'.  */
  7989.     else if @table_qualifier = '' and
  7990.         @table_owner = '%' and
  7991.         @table_name = ''
  7992.     begin   /* If enumerating owners */
  7993.         select distinct
  7994.             TABLE_QUALIFIER = convert(varchar(32),null),
  7995.             TABLE_OWNER = convert(varchar(32),user_name(uid)),
  7996.             TABLE_NAME = convert(varchar(32),null),
  7997.             TABLE_TYPE = convert(varchar(32),null),
  7998.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  7999.         from sysobjects
  8000.         order by 2
  8001.     end
  8002.  
  8003.     /* Special feature #3:  enumerate table types when qualifier, owner and
  8004.          name are blank but table type is explicitly '%'.   */
  8005.     else if @table_qualifier = '' and
  8006.         @table_owner = '' and
  8007.         @table_name = '' and
  8008.         @table_type = '%'
  8009.     begin   /* If enumerating table types */
  8010.         select
  8011.             TABLE_QUALIFIER = convert(varchar(32),null),
  8012.             TABLE_OWNER = convert(varchar(32),null),
  8013.             TABLE_NAME = convert(varchar(32),null),
  8014.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  8015.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8016.         from sysobjects o, syscolumns c
  8017.         where o.id=c.id and o.name='sysusers' and colid<=3
  8018.     end
  8019.  
  8020.     else
  8021.     begin /* end of special features - do normal processing */
  8022.         if @table_qualifier is not null
  8023.         begin
  8024.             if db_name() <> @table_qualifier
  8025.             begin
  8026.                 if @table_qualifier = ''
  8027.                 begin  /* If empty qualifier supplied */
  8028.                     /* Force an empty result set */
  8029.                     select @table_name = ''
  8030.                     select @table_owner = ''
  8031.                 end
  8032.                 else
  8033.                 begin   /* If qualifier doesn't match current database */
  8034.                     raiserror 20001 '~~Rush_5~~'
  8035.                     return
  8036.                 end
  8037.             end
  8038.         end
  8039.         if @table_type is null
  8040.         begin   /* Select all ODBC supported table types */
  8041.             select @type1 = 'SUV'
  8042.         end
  8043.         else
  8044.         begin
  8045.             /*  TableType is case sensitive if CS server */
  8046.             select @type1 = null
  8047.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  8048.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  8049.             if (charindex('''TABLE''',@table_type) <> 0)
  8050.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  8051.             if (charindex('''VIEW''',@table_type) <> 0)
  8052.                 select @type1 = @type1 + 'V'    /* Add Views */
  8053.         end
  8054.         if @table_name is null
  8055.         begin   /*  If table name not supplied, match all */
  8056.             select @table_name = '%'
  8057.         end
  8058.         else
  8059.         begin
  8060.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  8061.             begin   /* If owner not specified and table is specified */
  8062.                 if exists (select * from sysobjects
  8063.                     where uid = user_id()
  8064.                     and name = @table_name
  8065.                     and (type = 'U' or type = 'V' or type = 'S'))
  8066.                 begin   /* Override supplied owner w/owner of table */
  8067.                     select @table_owner = user_name()
  8068.                 end
  8069.             end
  8070.         end
  8071.         if @table_owner is null /* If no owner supplied, force wildcard */
  8072.             select @table_owner = '%'
  8073.         select
  8074.             TABLE_QUALIFIER = convert(varchar(32),db_name()),
  8075.             TABLE_OWNER = convert(varchar(32),user_name(o.uid)),
  8076.             TABLE_NAME = convert(varchar(32),o.name),   /* make nullable */
  8077.             TABLE_TYPE = convert(varchar(32),rtrim(
  8078.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  8079.                     (ascii(o.type)-83)*12+1,12))),  /* 'S'=0,'U'=2,'V'=3 */
  8080.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8081.         from sysusers u, sysobjects o
  8082.         where
  8083.             o.name like @table_name
  8084.             and user_name(o.uid) like @table_owner
  8085.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  8086.             and u.uid = user_id() /* constrain sysusers uid for use in subquery */
  8087.             and (
  8088.                 suser_id() = 1   /* User is the System Administrator */
  8089.                 or o.uid = user_id()     /* User created the object */
  8090.                 /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  8091.                 or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  8092.                     from sysprotects p
  8093.                     /* outer join to correlate with all rows in sysobjects */
  8094.                     where p.id =* o.id
  8095.                         /* get rows for public,current user,user's group */
  8096.                         and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  8097.                         /* check for SELECT,EXECUTE privilege */
  8098.                         and (action in (193,224)))&1     /* more magic...normalize GRANT */
  8099.                     ) = 1   /* final magic...compare Grants   */
  8100.             )
  8101.         order by 4, 1, 2, 3
  8102.     end
  8103. go
  8104.  
  8105. if (charindex('7.00', @@version) > 0 or
  8106.     charindex('8.00', @@version) > 0)
  8107.     drop procedure sp_tables
  8108. else
  8109. begin
  8110.     print ''
  8111.     print ''
  8112.     print 'Warning:'
  8113.     print 'you are installing the stored procedures '
  8114.     print 'on a pre 8.0 SQL Server.'
  8115.     print 'Ignore the following errors.'
  8116. end
  8117. go
  8118.  
  8119. /*  Procedure for 8.0 server */
  8120. create procedure sp_tables(
  8121.                @table_name      nvarchar(384)   = null,
  8122.                @table_owner     nvarchar(384)   = null,
  8123.                @table_qualifier sysname = null,
  8124.                @table_type      varchar(100) = null)
  8125. as
  8126.     declare @type1 varchar(3)
  8127.     declare @tableindex int
  8128.  
  8129.  
  8130.     /* Special feature #1:  enumerate databases when owner and name
  8131.          are blank but qualifier is explicitly '%'.  */
  8132.     if @table_qualifier = '%' and
  8133.         @table_owner = '' and
  8134.         @table_name = ''
  8135.     begin   /* If enumerating databases */
  8136.         select
  8137.             TABLE_QUALIFIER = convert(sysname,d.name),
  8138.             TABLE_OWNER = convert(sysname,null),
  8139.             TABLE_NAME = convert(sysname,null),
  8140.             TABLE_TYPE = convert(varchar(32),null),
  8141.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8142.         from master.dbo.sysdatabases d
  8143.         where d.name <> 'model' /* eliminate MODEL database */
  8144.         order by 1
  8145.     end
  8146.  
  8147.     /* Special feature #2:  enumerate owners when qualifier and name
  8148.          are blank but owner is explicitly '%'.  */
  8149.     else if @table_qualifier = '' and
  8150.         @table_owner = '%' and
  8151.         @table_name = ''
  8152.     begin   /* If enumerating owners */
  8153.         select distinct
  8154.             TABLE_QUALIFIER = convert(sysname,null),
  8155.             TABLE_OWNER = convert(sysname,user_name(uid)),
  8156.             TABLE_NAME = convert(sysname,null),
  8157.             TABLE_TYPE = convert(varchar(32),null),
  8158.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8159.         from sysobjects
  8160.         order by 2
  8161.     end
  8162.  
  8163.     /* Special feature #3:  enumerate table types when qualifier, owner and
  8164.          name are blank but table type is explicitly '%'.   */
  8165.     else if @table_qualifier = '' and
  8166.         @table_owner = '' and
  8167.         @table_name = '' and
  8168.         @table_type = '%'
  8169.     begin   /* If enumerating table types */
  8170.         select
  8171.             TABLE_QUALIFIER = convert(sysname,null),
  8172.             TABLE_OWNER = convert(sysname,null),
  8173.             TABLE_NAME = convert(sysname,null),
  8174.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  8175.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8176.         from sysobjects o, syscolumns c
  8177.         where o.id=c.id and o.name='sysusers' and colid<=3
  8178.     end
  8179.  
  8180.     else
  8181.     begin /* end of special features - do normal processing */
  8182.         if @table_qualifier is not null
  8183.         begin
  8184.             if db_name() <> @table_qualifier
  8185.             begin
  8186.                 if @table_qualifier = ''
  8187.                 begin  /* If empty qualifier supplied */
  8188.                     /* Force an empty result set */
  8189.                     select @table_name = ''
  8190.                     select @table_owner = ''
  8191.                 end
  8192.                 else
  8193.                 begin   /* If qualifier doesn't match current database */
  8194.                     raiserror (15250, -1,-1)
  8195.                     return
  8196.                 end
  8197.             end
  8198.         end
  8199.         if @table_type is null
  8200.         begin   /* Select all ODBC supported table types */
  8201.             select @type1 = 'SUV'
  8202.         end
  8203.         else
  8204.         begin
  8205.             /*  TableType is case sensitive if CS server */
  8206.             select @type1 = ''
  8207.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  8208.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  8209.             if (charindex('''TABLE''',@table_type) <> 0)
  8210.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  8211.             if (charindex('''VIEW''',@table_type) <> 0)
  8212.                 select @type1 = @type1 + 'V'    /* Add Views */
  8213.         end
  8214.         if @table_name is null
  8215.         begin   /*  If table name not supplied, match all */
  8216.             select @table_name = '%'
  8217.         end
  8218.         else
  8219.         begin
  8220.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  8221.             begin   /* If owner not specified and table is specified */
  8222.                 if exists (select * from sysobjects
  8223.                     where uid = user_id()
  8224.                     and name = @table_name
  8225.                     and (type = 'U' or type = 'V' or type = 'S'))
  8226.                 begin   /* Override supplied owner w/owner of table */
  8227.                     select @table_owner = user_name()
  8228.                 end
  8229.             end
  8230.         end
  8231.         if @table_owner is null /* If no owner supplied, force wildcard */
  8232.             select @table_owner = '%'
  8233.         select
  8234.             TABLE_QUALIFIER = convert(sysname,db_name()),
  8235.             TABLE_OWNER = convert(sysname,user_name(o.uid)),
  8236.             TABLE_NAME = convert(sysname,o.name),   /* make nullable */
  8237.             TABLE_TYPE = convert(varchar(32),rtrim(
  8238.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  8239.                     (ascii(o.type)-83)*12+1,12))),  /* 'S'=0,'U'=2,'V'=3 */
  8240.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8241.         from sysobjects o
  8242.         where
  8243.             o.name like @table_name
  8244.             and user_name(o.uid) like @table_owner
  8245.             and o.type in ('U','V','S')
  8246.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  8247.             and permissions (o.id)&4096 <> 0
  8248.         order by 4, 1, 2, 3
  8249.     end
  8250. go
  8251.  
  8252.  
  8253. grant execute on sp_tables to public
  8254. go
  8255.  
  8256. dump tran master with no_log
  8257. go
  8258.  
  8259. /*-----------------------------------------------------------------------------*/
  8260. /*-------------- CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  8261. /*-----------------------------------------------------------------------------*/
  8262.  
  8263. if object_id('sp_linkedservers', 'P') is not null
  8264.     drop proc sp_linkedservers
  8265. go
  8266. raiserror(15339,-1,-1,'sp_linkedservers')
  8267. go
  8268. create proc sp_linkedservers as
  8269.     select
  8270.         SRV_NAME = srvname,
  8271.         SRV_PROVIDERNAME = providername,
  8272.         SRV_PRODUCT = srvproduct,
  8273.         SRV_DATASOURCE = datasource,
  8274.         SRV_PROVIDERSTRING = providerstring,
  8275.         SRV_LOCATION = location,
  8276.         SRV_CAT = catalog
  8277.     from master.dbo.sysservers
  8278.     order by 1
  8279. go
  8280. grant execute on sp_linkedservers to public
  8281. go
  8282.  
  8283.  
  8284. if object_id('sp_catalogs', 'P') is not null
  8285.     drop proc sp_catalogs
  8286. go
  8287. raiserror(15339,-1,-1,'sp_catalogs')
  8288. go
  8289. create procedure sp_catalogs(
  8290.     @server_name        sysname)
  8291. as
  8292.     select
  8293.         CATALOG_NAME,
  8294.         convert (nvarchar(255),DESCRIPTION)
  8295.     from master.dbo.SYSREMOTE_CATALOGS < @server_name >
  8296.     order by CATALOG_NAME
  8297. go
  8298. grant execute on sp_catalogs to public
  8299. go
  8300.  
  8301.  
  8302. if object_id('sp_tables_ex', 'P') is not null
  8303.     drop proc sp_tables_ex
  8304. go
  8305. raiserror(15339,-1,-1,'sp_tables_ex')
  8306. go
  8307. create procedure sp_tables_ex(
  8308.     @table_server       sysname,
  8309.     @table_name         sysname = null,
  8310.     @table_schema       sysname = null,
  8311.     @table_catalog      sysname = null,
  8312.     @table_type         sysname = null)
  8313. as
  8314.     declare @table_catalog_param        sysname
  8315.     if ((isnull(charindex('%', @table_catalog),0) = 0) and
  8316.         (isnull(charindex('[', @table_catalog),0) = 0) and
  8317.         (isnull(charindex('_', @table_catalog),0) = 0))
  8318.         select @table_catalog_param = @table_catalog
  8319.     else
  8320.         select @table_catalog_param = null
  8321.  
  8322.     if ((isnull(charindex('%', @table_name),0) = 0) and
  8323.         (isnull(charindex('[', @table_name),0) = 0) and
  8324.         (isnull(charindex('_', @table_name),0) = 0))
  8325.     begin   /*  If no wild carding */
  8326.         select
  8327.             TABLE_CAT = TABLE_CATALOG,
  8328.             TABLE_SCHEM = TABLE_SCHEMA,
  8329.             TABLE_NAME = TABLE_NAME,
  8330.             TABLE_TYPE = TABLE_TYPE,
  8331.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  8332.         from master.dbo.SYSREMOTE_TABLES <
  8333.                     @table_server,
  8334.                     @table_catalog_param,
  8335.                     NULL,
  8336.                     @table_name,
  8337.                     NULL >
  8338.         where (TABLE_SCHEMA like @table_schema
  8339.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8340.             and (TABLE_CATALOG like @table_catalog
  8341.             or   @table_catalog is NULL or (TABLE_CATALOG is NULL and @table_catalog = N'%'))
  8342.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  8343.             or   @table_type is NULL)
  8344.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  8345.     end
  8346.     else
  8347.     begin   /*  If wild carding */
  8348.         select
  8349.             TABLE_CAT = TABLE_CATALOG,
  8350.             TABLE_SCHEM = TABLE_SCHEMA,
  8351.             TABLE_NAME = TABLE_NAME,
  8352.             TABLE_TYPE = TABLE_TYPE,
  8353.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  8354.         from master.dbo.SYSREMOTE_TABLES <
  8355.                     @table_server,
  8356.                     @table_catalog_param,
  8357.                     NULL,
  8358.                     NULL,
  8359.                     NULL >
  8360.         where (TABLE_SCHEMA like @table_schema
  8361.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8362.             and (TABLE_CATALOG like @table_catalog
  8363.             or   @table_catalog is NULL or (TABLE_CATALOG is NULL and @table_catalog = N'%'))
  8364.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  8365.             or   @table_type is NULL)
  8366.             and TABLE_NAME like @table_name
  8367.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  8368.     end
  8369. go
  8370. grant execute on sp_tables_ex to public
  8371. go
  8372.  
  8373.  
  8374. if object_id('sp_columns_ex', 'P') is not null
  8375.     drop proc sp_columns_ex
  8376. go
  8377.  
  8378. /* 7.0 version */
  8379. raiserror(15339,-1,-1,'sp_columns_ex')
  8380. go
  8381.  
  8382. create procedure sp_columns_ex(
  8383.     @table_server       sysname,
  8384.     @table_name         sysname = null,
  8385.     @table_schema       sysname = null,
  8386.     @table_catalog      sysname = null,
  8387.     @column_name        sysname = null,
  8388.     @ODBCVer            int = 2)
  8389. as
  8390.  
  8391.     set nocount on
  8392.     declare
  8393.     @DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1), @DBCOLUMNFLAGS_ISLONG   binary(1),
  8394.     @DBCOLUMNFLAGS_ISROWVER binary(2)
  8395.  
  8396.     select
  8397.     @DBCOLUMNFLAGS_ISFIXEDLENGTH    = 0x10, @DBCOLUMNFLAGS_ISLONG   = 0x80,
  8398.     @DBCOLUMNFLAGS_ISROWVER = 0x0200
  8399.  
  8400.     declare 
  8401.     @DBTYPE_I2 smallint,            @DBTYPE_I4 smallint,
  8402.     @DBTYPE_R4 smallint,            @DBTYPE_R8 smallint,
  8403.     @DBTYPE_CY smallint,            @DBTYPE_DATE smallint,
  8404.     @DBTYPE_BSTR smallint,          @DBTYPE_BOOL smallint,
  8405.     @DBTYPE_VARNUMERIC smallint,    @DBTYPE_DECIMAL smallint,
  8406.     @DBTYPE_UI1 smallint,           @DBTYPE_BYREF smallint,
  8407.     @DBTYPE_I1 smallint,            @DBTYPE_UI2 smallint,
  8408.     @DBTYPE_UI4 smallint,           @DBTYPE_I8 smallint,
  8409.     @DBTYPE_UI8 smallint,           @DBTYPE_GUID smallint,
  8410.     @DBTYPE_BYTES smallint,         @DBTYPE_STR smallint,
  8411.     @DBTYPE_WSTR smallint,          @DBTYPE_NUMERIC smallint,
  8412.     @DBTYPE_DBDATE smallint,        @DBTYPE_DBTIME smallint,
  8413.     @DBTYPE_DBTIMESTAMP smallint
  8414.  
  8415.     select
  8416.     @DBTYPE_I2          = 2,    @DBTYPE_I4          = 3,
  8417.     @DBTYPE_R4          = 4,    @DBTYPE_R8          = 5,
  8418.     @DBTYPE_CY          = 6,    @DBTYPE_DATE        = 7,
  8419.     @DBTYPE_BSTR        = 8,    @DBTYPE_BOOL        = 11,
  8420.     @DBTYPE_VARNUMERIC  = 139,  @DBTYPE_DECIMAL     = 14,
  8421.     @DBTYPE_UI1         = 17,   @DBTYPE_BYREF       = 16384,
  8422.     @DBTYPE_I1          = 16,   @DBTYPE_UI2         = 18,
  8423.     @DBTYPE_UI4         = 19,   @DBTYPE_I8          = 20,
  8424.     @DBTYPE_UI8         = 21,   @DBTYPE_GUID        = 72,
  8425.     @DBTYPE_BYTES       = 128,  @DBTYPE_STR         = 129,
  8426.     @DBTYPE_WSTR        = 130,  @DBTYPE_NUMERIC     = 131,
  8427.     @DBTYPE_DBDATE      = 133,  @DBTYPE_DBTIME      = 134,  
  8428.     @DBTYPE_DBTIMESTAMP = 135
  8429.  
  8430.     declare 
  8431.     @ODS_SMALLINT_NOT_NULL tinyint, @ODS_INT_NULL tinyint,
  8432.     @ODS_INT_NOT_NULL tinyint,      @ODS_TINYINT_NOT_NULL tinyint,
  8433.     @ODS_REAL_NOT_NULL tinyint,     @ODS_FLOAT_NULL tinyint,
  8434.     @ODS_FLOAT_NOT_NULL tinyint,    @ODS_MONEY_NULL tinyint,
  8435.     @ODS_MONEY_NOT_NULL tinyint,    @ODS_DATETIME_NULL tinyint,
  8436.     @ODS_DATETIME_NOT_NULL tinyint, @ODS_TEXT tinyint,
  8437.     @ODS_BIT tinyint,               @ODS_DECIMAL tinyint,
  8438.     @ODS_NUMERIC tinyint,           @ODS_GUID tinyint,
  8439.     @ODS_IMAGE tinyint,             @ODS_TIMESTAMP tinyint,
  8440.     @ODS_BINARY_NULL tinyint,       @ODS_BINARY_NOT_NULL tinyint,
  8441.     @ODS_VARBINARY tinyint,         @ODS_CHAR_NULL tinyint,
  8442.     @ODS_CHAR_NOT_NULL tinyint,     @ODS_VARCHAR tinyint
  8443.  
  8444.     select  
  8445.     @ODS_IMAGE = 34,            @ODS_TEXT = 35,
  8446.     @ODS_GUID = 36,             @ODS_BINARY_NULL = 37,          
  8447.     @ODS_VARBINARY = 37,        @ODS_INT_NULL = 38,
  8448.     @ODS_CHAR_NULL = 39,        @ODS_VARCHAR = 39,
  8449.     @ODS_TIMESTAMP = 45,        @ODS_BINARY_NOT_NULL = 45,
  8450.     @ODS_CHAR_NOT_NULL = 47,    @ODS_TINYINT_NOT_NULL = 48,
  8451.     @ODS_BIT = 50,              @ODS_SMALLINT_NOT_NULL = 52,    
  8452.     @ODS_INT_NOT_NULL = 56,     @ODS_REAL_NOT_NULL = 59,        
  8453.     @ODS_MONEY_NOT_NULL = 60,   @ODS_DATETIME_NOT_NULL = 61,    
  8454.     @ODS_FLOAT_NOT_NULL = 62,   @ODS_DECIMAL = 106,
  8455.     @ODS_NUMERIC = 108,         @ODS_FLOAT_NULL = 109,
  8456.     @ODS_MONEY_NULL = 110,      @ODS_DATETIME_NULL = 111
  8457.  
  8458.     declare 
  8459.     @ODBC_GUID smallint,        @ODBC_NTEXT smallint,
  8460.     @ODBC_NVARCHAR smallint,    @ODBC_NCHAR smallint,
  8461.     @ODBC_BIT smallint,         @ODBC_TINYINT smallint,
  8462.     @ODBC_IMAGE smallint,       @ODBC_VARBINARY smallint,
  8463.     @ODBC_TIMESTAMP smallint,   @ODBC_BINARY smallint,
  8464.     @ODBC_TEXT smallint,        @ODBC_CHAR smallint,
  8465.     @ODBC_NUMERIC smallint,     @ODBC_DECIMAL smallint,
  8466.     @ODBC_MONEY smallint,       @ODBC_INT smallint,     
  8467.     @ODBC_SMALLINT smallint,    @ODBC_FLOAT smallint,   
  8468.     @ODBC_REAL smallint,        @ODBC_VARCHAR smallint,
  8469.     @ODBC_DATETIME smallint
  8470.  
  8471.     select
  8472.     @ODBC_GUID      = -11,  @ODBC_NTEXT     = -10,
  8473.     @ODBC_NVARCHAR  = -9,   @ODBC_NCHAR     = -8,
  8474.     @ODBC_BIT       = -7,   @ODBC_TINYINT   = -6,
  8475.     @ODBC_IMAGE     = -4,   @ODBC_VARBINARY = -3,
  8476.     @ODBC_TIMESTAMP = -2,   @ODBC_BINARY    = -2,
  8477.     @ODBC_TEXT      = -1,   @ODBC_CHAR      = 1,
  8478.     @ODBC_NUMERIC   = 2,    @ODBC_DECIMAL   = 3,
  8479.     @ODBC_MONEY     = 3,    @ODBC_INT       = 4,        
  8480.     @ODBC_SMALLINT  = 5,    @ODBC_FLOAT     = 6,    
  8481.     @ODBC_REAL      = 7,    @ODBC_VARCHAR   = 12,
  8482.     @ODBC_DATETIME  =   
  8483.     case @ODBCVer
  8484.         when 2 then 11
  8485.         else 93
  8486.     end
  8487.  
  8488.     create table #tmp_columns
  8489.     (   TABLE_CAT sysname  NULL,
  8490.         TABLE_SCHEM sysname  NULL,
  8491.         TABLE_NAME sysname NOT NULL,
  8492.         COLUMN_NAME sysname  NULL,
  8493.         DATA_TYPE smallint NOT NULL,
  8494.         TYPE_NAME sysname   NULL,
  8495.         COLUMN_SIZE int NULL,
  8496.         BUFFER_LENGTH int NULL,
  8497.         DECIMAL_DIGITS smallint NULL,
  8498.         NUM_PREC_RADIX smallint NULL,
  8499.         NULLABLE smallint NOT NULL,
  8500.         REMARKS nvarchar(255)  NULL,
  8501.             COLUMN_DEF nvarchar(128)  NULL,
  8502.             SQL_DATA_TYPE smallint null,
  8503.             SQL_DATETIME_SUB smallint NULL,
  8504.         CHAR_OCTET_LENGTH int NULL,
  8505.         ORDINAL_POSITION smallint,
  8506.         IS_NULLABLE varchar(254)  NOT NULL,
  8507.         SS_DATA_TYPE tinyint null,
  8508.         COLUMN_FLAGS int NOT NULL)
  8509.  
  8510.     if ((isnull(charindex('%', @table_name),0) = 0) and
  8511.         (isnull(charindex('[', @table_name),0) = 0) and
  8512.         (isnull(charindex('_', @table_name),0) = 0))
  8513.     begin   /*  If no wild carding */
  8514.         insert into #tmp_columns
  8515.         select
  8516.             TABLE_CAT = c.TABLE_CATALOG,
  8517.             TABLE_SCHEM = c.TABLE_SCHEMA,
  8518.             TABLE_NAME = c.TABLE_NAME,
  8519.             COLUMN_NAME = c.COLUMN_NAME,
  8520.             DATA_TYPE =
  8521.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  8522.                 when @DBTYPE_I2             then @ODBC_SMALLINT
  8523.                 when @DBTYPE_I4             then @ODBC_INT
  8524.                 when @DBTYPE_R4             then @ODBC_REAL
  8525.                 when @DBTYPE_R8             then @ODBC_FLOAT
  8526.                 when @DBTYPE_CY             then @ODBC_MONEY
  8527.                 when @DBTYPE_DATE           then @ODBC_DATETIME
  8528.                 when @DBTYPE_DBDATE         then @ODBC_DATETIME
  8529.                 when @DBTYPE_DBTIME         then @ODBC_DATETIME
  8530.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  8531.                 when @DBTYPE_BOOL           then @ODBC_BIT
  8532.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  8533.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  8534.                 when @DBTYPE_I1             then @ODBC_NUMERIC
  8535.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  8536.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  8537.                 when @DBTYPE_I8             then @ODBC_NUMERIC
  8538.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  8539.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  8540.                 when @DBTYPE_VARNUMERIC     then @ODBC_NUMERIC
  8541.                 when @DBTYPE_GUID           then @ODBC_GUID
  8542.                 when @DBTYPE_BYTES then
  8543.                     case
  8544.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  8545.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8546.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8547.                                  p.COLUMN_SIZE = 8
  8548.                             then @ODBC_TIMESTAMP
  8549.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8550.                             then @ODBC_BINARY
  8551.                         else @ODBC_VARBINARY
  8552.                     end
  8553.                 when @DBTYPE_STR then
  8554.                     case
  8555.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  8556.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  8557.                         else @ODBC_VARCHAR
  8558.                     end
  8559.                 when @DBTYPE_WSTR then
  8560.                     case
  8561.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8562.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8563.                         else @ODBC_NVARCHAR
  8564.                     end
  8565.                 when @DBTYPE_BSTR then
  8566.                     case
  8567.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8568.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8569.                         else @ODBC_NVARCHAR
  8570.                     end
  8571.                 else @ODBC_NVARCHAR     /*  Unknown OleDB datatype */
  8572.             end,
  8573.             TYPE_NAME = p.TYPE_NAME,
  8574.             COLUMN_SIZE =
  8575.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  8576.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8577.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  8578.             NUM_PREC_RADIX = NULL,
  8579.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  8580.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  8581.             COLUMN_DEF = c.COLUMN_DEFAULT,
  8582.             SQL_DATA_TYPE = NULL,
  8583.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  8584.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8585.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  8586.             IS_NULLABLE =
  8587.             case c.IS_NULLABLE
  8588.                 when 1 then 'YES'
  8589.                 else 'NO'
  8590.             end,
  8591.             SS_DATA_TYPE = NULL,
  8592.             COLUMN_FLAGS = c.COLUMN_FLAGS
  8593.         from master.dbo.SYSREMOTE_COLUMNS <
  8594.                     @table_server,
  8595.                     @table_catalog,
  8596.                     NULL,
  8597.                     @table_name,
  8598.                     NULL > c,
  8599.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  8600.                     @table_server > p
  8601.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  8602.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  8603.                 (TABLE_SCHEMA like @table_schema
  8604.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8605.                 and (c.COLUMN_NAME like @column_name
  8606.                 or   @column_name is NULL)
  8607.     end
  8608.     else
  8609.     begin   /*  If wild carding */
  8610.         insert into #tmp_columns
  8611.         select
  8612.             TABLE_CAT = c.TABLE_CATALOG,
  8613.             TABLE_SCHEM = c.TABLE_SCHEMA,
  8614.             TABLE_NAME = c.TABLE_NAME,
  8615.             COLUMN_NAME = c.COLUMN_NAME,
  8616.             DATA_TYPE =
  8617.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  8618.                 when @DBTYPE_I2             then @ODBC_SMALLINT
  8619.                 when @DBTYPE_I4             then @ODBC_INT
  8620.                 when @DBTYPE_R4             then @ODBC_REAL
  8621.                 when @DBTYPE_R8             then @ODBC_FLOAT
  8622.                 when @DBTYPE_CY             then @ODBC_MONEY
  8623.                 when @DBTYPE_DATE           then @ODBC_DATETIME
  8624.                 when @DBTYPE_DBDATE         then @ODBC_DATETIME
  8625.                 when @DBTYPE_DBTIME         then @ODBC_DATETIME
  8626.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  8627.                 when @DBTYPE_BOOL           then @ODBC_BIT
  8628.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  8629.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  8630.                 when @DBTYPE_I1             then @ODBC_NUMERIC
  8631.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  8632.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  8633.                 when @DBTYPE_I8             then @ODBC_NUMERIC
  8634.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  8635.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  8636.                 when @DBTYPE_VARNUMERIC     then @ODBC_NUMERIC
  8637.                 when @DBTYPE_GUID           then @ODBC_GUID
  8638.                 when @DBTYPE_BYTES then
  8639.                     case
  8640.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  8641.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8642.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8643.                                  p.COLUMN_SIZE = 8
  8644.                             then @ODBC_TIMESTAMP
  8645.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8646.                             then @ODBC_BINARY
  8647.                         else @ODBC_VARBINARY
  8648.                     end
  8649.                 when @DBTYPE_STR then
  8650.                     case
  8651.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  8652.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  8653.                         else @ODBC_VARCHAR
  8654.                     end
  8655.                 when @DBTYPE_WSTR then
  8656.                     case
  8657.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8658.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8659.                         else @ODBC_NVARCHAR
  8660.                     end
  8661.                 when @DBTYPE_BSTR then
  8662.                     case
  8663.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8664.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8665.                         else @ODBC_NVARCHAR
  8666.                     end
  8667.                 else @ODBC_NVARCHAR     /*  Unknown OleDB datatype */
  8668.             end,
  8669.             TYPE_NAME = p.TYPE_NAME,
  8670.             COLUMN_SIZE =
  8671.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  8672.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8673.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  8674.             NUM_PREC_RADIX = NULL,
  8675.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  8676.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  8677.             COLUMN_DEF = c.COLUMN_DEFAULT,
  8678.             SQL_DATA_TYPE = NULL,
  8679.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  8680.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8681.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  8682.             IS_NULLABLE =
  8683.             case c.IS_NULLABLE
  8684.                 when 1 then 'YES'
  8685.                 else 'NO'
  8686.             end,
  8687.             SS_DATA_TYPE = NULL,
  8688.             COLUMN_FLAGS = c.COLUMN_FLAGS
  8689.         from master.dbo.SYSREMOTE_COLUMNS <
  8690.                     @table_server,
  8691.                     @table_catalog,
  8692.                     NULL,
  8693.                     NULL,
  8694.                     NULL > c,
  8695.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  8696.                     @table_server > p
  8697.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  8698.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  8699.                 (TABLE_SCHEMA like @table_schema
  8700.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8701.                 and (c.COLUMN_NAME like @column_name
  8702.                 or   @column_name is NULL)
  8703.                 and c.TABLE_NAME like @table_name
  8704.     end
  8705.  
  8706.     update #tmp_columns
  8707.     set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
  8708.         SQL_DATETIME_SUB =
  8709.         case
  8710.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME then 3
  8711.             else #tmp_columns.SQL_DATETIME_SUB
  8712.         end,
  8713.         TYPE_NAME = 
  8714.         case 
  8715.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8716.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8717.                     BUFFER_LENGTH = 8
  8718.                 then 'timestamp'
  8719.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  8720.                     #tmp_columns.SQL_DATETIME_SUB = 0
  8721.                 then 'smalldatetime'
  8722.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and
  8723.                     #tmp_columns.COLUMN_SIZE = 10
  8724.                 then 'smallmoney'
  8725.             when (#tmp_columns.DATA_TYPE in (@ODBC_BINARY, @ODBC_CHAR) and
  8726.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH) or
  8727.                  #tmp_columns.DATA_TYPE in (@ODBC_TEXT, @ODBC_NTEXT, @ODBC_IMAGE)
  8728.                 then spt_dt.TYPE_NAME
  8729.             else isnull(#tmp_columns.TYPE_NAME, spt_dt.TYPE_NAME)
  8730.         end,
  8731.         COLUMN_SIZE =
  8732.         case
  8733.             when #tmp_columns.DATA_TYPE IN (@ODBC_REAL, @ODBC_FLOAT) then /* app. numeric types */
  8734.                 spt_dt.data_precision
  8735.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  8736.                     #tmp_columns.SQL_DATETIME_SUB = 0
  8737.                 then 16
  8738.             else #tmp_columns.COLUMN_SIZE
  8739.         end,
  8740.         BUFFER_LENGTH =
  8741.         case
  8742.             when #tmp_columns.DATA_TYPE IN (@ODBC_NUMERIC, @ODBC_DECIMAL) then  /* decimal/numeric types */
  8743.                 COLUMN_SIZE+2
  8744.             when isnull (#tmp_columns.BUFFER_LENGTH,0) = 0 then spt_dt.length
  8745.             else #tmp_columns.BUFFER_LENGTH
  8746.         end,
  8747.         DECIMAL_DIGITS =
  8748.         case
  8749.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME
  8750.                 then #tmp_columns.SQL_DATETIME_SUB
  8751.             else COALESCE (DECIMAL_DIGITS, spt_dt.numeric_scale)
  8752.         end,
  8753.         NUM_PREC_RADIX = spt_dt.RADIX,
  8754.         SS_DATA_TYPE = 
  8755.         case
  8756.             when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
  8757.                 and #tmp_columns.NULLABLE = 1
  8758.                     then @ODS_INT_NULL
  8759.             when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
  8760.                 then @ODS_TINYINT_NOT_NULL
  8761.             when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT    and #tmp_columns.NULLABLE = 0
  8762.                 then @ODS_SMALLINT_NOT_NULL
  8763.             when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
  8764.                 then @ODS_INT_NOT_NULL
  8765.             when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
  8766.                 then @ODS_FLOAT_NULL
  8767.             when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
  8768.                 then @ODS_REAL_NOT_NULL
  8769.             when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
  8770.                 then @ODS_FLOAT_NOT_NULL
  8771.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
  8772.                 then @ODS_MONEY_NULL
  8773.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
  8774.                 then @ODS_MONEY_NOT_NULL
  8775.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
  8776.                 then @ODS_DATETIME_NULL
  8777.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
  8778.                 then @ODS_DATETIME_NOT_NULL
  8779.             when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
  8780.             when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
  8781.             when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL  
  8782.             when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
  8783.             when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
  8784.             when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
  8785.             when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
  8786.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8787.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8788.                     BUFFER_LENGTH = 8
  8789.                 then @ODS_TIMESTAMP
  8790.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =  1
  8791.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8792.                 then  @ODS_BINARY_NULL
  8793.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =  0
  8794.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8795.                 then @ODS_BINARY_NOT_NULL
  8796.             when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
  8797.                 then @ODS_VARBINARY 
  8798.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
  8799.                 then @ODS_CHAR_NULL
  8800.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
  8801.                 then @ODS_CHAR_NOT_NULL
  8802.             when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
  8803.             else null
  8804.         end
  8805.         from master.dbo.spt_datatype_info spt_dt
  8806.         where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
  8807.             AND (spt_dt.ODBCVer is null or spt_dt.ODBCVer = @ODBCVer)
  8808.             and isnull(spt_dt.AUTO_INCREMENT,0) = 0
  8809.             and not spt_dt.TYPE_NAME = 'smalldatetime'
  8810.  
  8811.     select
  8812.             TABLE_CAT,          TABLE_SCHEM,        TABLE_NAME,
  8813.             COLUMN_NAME,        DATA_TYPE,          TYPE_NAME,
  8814.             COLUMN_SIZE,        BUFFER_LENGTH,      DECIMAL_DIGITS,
  8815.             NUM_PREC_RADIX,     NULLABLE,           REMARKS,
  8816.             COLUMN_DEF,         SQL_DATA_TYPE,      SQL_DATETIME_SUB,
  8817.             CHAR_OCTET_LENGTH,  ORDINAL_POSITION,   IS_NULLABLE,
  8818.             SS_DATA_TYPE
  8819.         from #tmp_columns
  8820.         order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  8821.  
  8822.         drop table #tmp_columns
  8823.  
  8824. go
  8825. if (charindex('8.00', @@version) > 0)
  8826.     drop procedure sp_columns_ex
  8827. else
  8828. begin
  8829.     print ''
  8830.     print ''
  8831.     print 'Warning:'
  8832.     print 'you are installing the stored procedures '
  8833.     print 'on a pre 8.0 SQL Server.'
  8834.     print 'Ignore the following errors.'
  8835. end
  8836. go
  8837.  
  8838. /* 8.0 version */
  8839. raiserror(15339,-1,-1,'sp_columns_ex')
  8840. go
  8841. create procedure sp_columns_ex(
  8842.     @table_server       sysname,
  8843.     @table_name         sysname = null,
  8844.     @table_schema       sysname = null,
  8845.     @table_catalog      sysname = null,
  8846.     @column_name        sysname = null,
  8847.     @ODBCVer            int = 2)
  8848. as
  8849.  
  8850.     set nocount on
  8851.     declare
  8852.     @DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1), @DBCOLUMNFLAGS_ISLONG   binary(1),
  8853.     @DBCOLUMNFLAGS_ISROWVER binary(2)
  8854.  
  8855.     select
  8856.     @DBCOLUMNFLAGS_ISFIXEDLENGTH    = 0x10, @DBCOLUMNFLAGS_ISLONG   = 0x80,
  8857.     @DBCOLUMNFLAGS_ISROWVER = 0x0200
  8858.  
  8859.     declare 
  8860.     @DBTYPE_I2 smallint,            @DBTYPE_I4 smallint,
  8861.     @DBTYPE_R4 smallint,            @DBTYPE_R8 smallint,
  8862.     @DBTYPE_CY smallint,            @DBTYPE_DATE smallint,
  8863.     @DBTYPE_BSTR smallint,          @DBTYPE_BOOL smallint,
  8864.     @DBTYPE_VARNUMERIC smallint,    @DBTYPE_DECIMAL smallint,
  8865.     @DBTYPE_UI1 smallint,           @DBTYPE_BYREF smallint,
  8866.     @DBTYPE_I1 smallint,            @DBTYPE_UI2 smallint,
  8867.     @DBTYPE_UI4 smallint,           @DBTYPE_I8 smallint,
  8868.     @DBTYPE_UI8 smallint,           @DBTYPE_GUID smallint,
  8869.     @DBTYPE_BYTES smallint,         @DBTYPE_STR smallint,
  8870.     @DBTYPE_WSTR smallint,          @DBTYPE_NUMERIC smallint,
  8871.     @DBTYPE_DBDATE smallint,        @DBTYPE_DBTIME smallint,
  8872.     @DBTYPE_DBTIMESTAMP smallint
  8873.  
  8874.     select
  8875.     @DBTYPE_I2          = 2,    @DBTYPE_I4          = 3,
  8876.     @DBTYPE_R4          = 4,    @DBTYPE_R8          = 5,
  8877.     @DBTYPE_CY          = 6,    @DBTYPE_DATE        = 7,
  8878.     @DBTYPE_BSTR        = 8,    @DBTYPE_BOOL        = 11,
  8879.     @DBTYPE_VARNUMERIC  = 139,  @DBTYPE_DECIMAL     = 14,
  8880.     @DBTYPE_UI1         = 17,   @DBTYPE_BYREF       = 16384,
  8881.     @DBTYPE_I1          = 16,   @DBTYPE_UI2         = 18,
  8882.     @DBTYPE_UI4         = 19,   @DBTYPE_I8          = 20,
  8883.     @DBTYPE_UI8         = 21,   @DBTYPE_GUID        = 72,
  8884.     @DBTYPE_BYTES       = 128,  @DBTYPE_STR         = 129,
  8885.     @DBTYPE_WSTR        = 130,  @DBTYPE_NUMERIC     = 131,
  8886.     @DBTYPE_DBDATE      = 133,  @DBTYPE_DBTIME      = 134,  
  8887.     @DBTYPE_DBTIMESTAMP = 135
  8888.  
  8889.     declare 
  8890.     @ODS_SMALLINT_NOT_NULL tinyint, @ODS_INT_NULL tinyint,
  8891.     @ODS_INT_NOT_NULL tinyint,      @ODS_TINYINT_NOT_NULL tinyint,
  8892.     @ODS_REAL_NOT_NULL tinyint,     @ODS_FLOAT_NULL tinyint,
  8893.     @ODS_FLOAT_NOT_NULL tinyint,    @ODS_MONEY_NULL tinyint,
  8894.     @ODS_MONEY_NOT_NULL tinyint,    @ODS_DATETIME_NULL tinyint,
  8895.     @ODS_DATETIME_NOT_NULL tinyint, @ODS_TEXT tinyint,
  8896.     @ODS_BIT tinyint,               @ODS_DECIMAL tinyint,
  8897.     @ODS_NUMERIC tinyint,           @ODS_GUID tinyint,
  8898.     @ODS_IMAGE tinyint,             @ODS_TIMESTAMP tinyint,
  8899.     @ODS_BINARY_NULL tinyint,       @ODS_BINARY_NOT_NULL tinyint,
  8900.     @ODS_VARBINARY tinyint,         @ODS_CHAR_NULL tinyint,
  8901.     @ODS_CHAR_NOT_NULL tinyint,     @ODS_VARCHAR tinyint
  8902.  
  8903.     select  
  8904.     @ODS_IMAGE = 34,            @ODS_TEXT = 35,
  8905.     @ODS_GUID = 36,             @ODS_BINARY_NULL = 37,          
  8906.     @ODS_VARBINARY = 37,        @ODS_INT_NULL = 38,
  8907.     @ODS_CHAR_NULL = 39,        @ODS_VARCHAR = 39,
  8908.     @ODS_TIMESTAMP = 45,        @ODS_BINARY_NOT_NULL = 45,
  8909.     @ODS_CHAR_NOT_NULL = 47,    @ODS_TINYINT_NOT_NULL = 48,
  8910.     @ODS_BIT = 50,              @ODS_SMALLINT_NOT_NULL = 52,    
  8911.     @ODS_INT_NOT_NULL = 56,     @ODS_REAL_NOT_NULL = 59,        
  8912.     @ODS_MONEY_NOT_NULL = 60,   @ODS_DATETIME_NOT_NULL = 61,    
  8913.     @ODS_FLOAT_NOT_NULL = 62,   @ODS_DECIMAL = 106,
  8914.     @ODS_NUMERIC = 108,         @ODS_FLOAT_NULL = 109,
  8915.     @ODS_MONEY_NULL = 110,      @ODS_DATETIME_NULL = 111
  8916.  
  8917.     declare 
  8918.     @ODBC_GUID smallint,        @ODBC_NTEXT smallint,
  8919.     @ODBC_NVARCHAR smallint,    @ODBC_NCHAR smallint,
  8920.     @ODBC_BIT smallint,         @ODBC_TINYINT smallint,
  8921.     @ODBC_IMAGE smallint,       @ODBC_VARBINARY smallint,
  8922.     @ODBC_TIMESTAMP smallint,   @ODBC_BINARY smallint,
  8923.     @ODBC_TEXT smallint,        @ODBC_CHAR smallint,
  8924.     @ODBC_NUMERIC smallint,     @ODBC_DECIMAL smallint,
  8925.     @ODBC_MONEY smallint,       @ODBC_INT smallint,     
  8926.     @ODBC_SMALLINT smallint,    @ODBC_FLOAT smallint,   
  8927.     @ODBC_REAL smallint,        @ODBC_VARCHAR smallint,
  8928.     @ODBC_DATETIME smallint
  8929.  
  8930.     select
  8931.     @ODBC_GUID      = -11,  @ODBC_NTEXT     = -10,
  8932.     @ODBC_NVARCHAR  = -9,   @ODBC_NCHAR     = -8,
  8933.     @ODBC_BIT       = -7,   @ODBC_TINYINT   = -6,
  8934.     @ODBC_IMAGE     = -4,   @ODBC_VARBINARY = -3,
  8935.     @ODBC_TIMESTAMP = -2,   @ODBC_BINARY    = -2,
  8936.     @ODBC_TEXT      = -1,   @ODBC_CHAR      = 1,
  8937.     @ODBC_NUMERIC   = 2,    @ODBC_DECIMAL   = 3,
  8938.     @ODBC_MONEY     = 3,    @ODBC_INT       = 4,        
  8939.     @ODBC_SMALLINT  = 5,    @ODBC_FLOAT     = 6,    
  8940.     @ODBC_REAL      = 7,    @ODBC_VARCHAR   = 12,
  8941.     @ODBC_DATETIME  =   
  8942.     case @ODBCVer
  8943.         when 2 then 11
  8944.         else 93
  8945.     end
  8946.  
  8947.     create table #tmp_columns
  8948.     (   TABLE_CAT sysname collate database_default NULL,
  8949.         TABLE_SCHEM sysname collate database_default NULL,
  8950.         TABLE_NAME sysname  collate database_default NOT NULL,
  8951.         COLUMN_NAME sysname collate database_default NULL,
  8952.         DATA_TYPE smallint NOT NULL,
  8953.         TYPE_NAME sysname  collate database_default NULL,
  8954.         COLUMN_SIZE int NULL,
  8955.         BUFFER_LENGTH int NULL,
  8956.         DECIMAL_DIGITS smallint NULL,
  8957.         NUM_PREC_RADIX smallint NULL,
  8958.         NULLABLE smallint NOT NULL,
  8959.         REMARKS nvarchar(255) collate database_default NULL,
  8960.             COLUMN_DEF nvarchar(128) collate database_default NULL,
  8961.             SQL_DATA_TYPE smallint null,
  8962.             SQL_DATETIME_SUB smallint NULL,
  8963.         CHAR_OCTET_LENGTH int NULL,
  8964.         ORDINAL_POSITION smallint,
  8965.         IS_NULLABLE varchar(254) collate database_default NOT NULL,
  8966.         SS_DATA_TYPE tinyint null,
  8967.         COLUMN_FLAGS int NOT NULL)
  8968.  
  8969.     if ((isnull(charindex('%', @table_name),0) = 0) and
  8970.         (isnull(charindex('[', @table_name),0) = 0) and
  8971.         (isnull(charindex('_', @table_name),0) = 0))
  8972.     begin   /*  If no wild carding */
  8973.         insert into #tmp_columns
  8974.         select
  8975.             TABLE_CAT = c.TABLE_CATALOG,
  8976.             TABLE_SCHEM = c.TABLE_SCHEMA,
  8977.             TABLE_NAME = c.TABLE_NAME,
  8978.             COLUMN_NAME = c.COLUMN_NAME,
  8979.             DATA_TYPE =
  8980.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  8981.                 when @DBTYPE_I2             then @ODBC_SMALLINT
  8982.                 when @DBTYPE_I4             then @ODBC_INT
  8983.                 when @DBTYPE_R4             then @ODBC_REAL
  8984.                 when @DBTYPE_R8             then @ODBC_FLOAT
  8985.                 when @DBTYPE_CY             then @ODBC_MONEY
  8986.                 when @DBTYPE_DATE           then @ODBC_DATETIME
  8987.                 when @DBTYPE_DBDATE         then @ODBC_DATETIME
  8988.                 when @DBTYPE_DBTIME         then @ODBC_DATETIME
  8989.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  8990.                 when @DBTYPE_BOOL           then @ODBC_BIT
  8991.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  8992.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  8993.                 when @DBTYPE_I1             then @ODBC_NUMERIC
  8994.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  8995.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  8996.                 when @DBTYPE_I8             then @ODBC_NUMERIC
  8997.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  8998.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  8999.                 when @DBTYPE_VARNUMERIC     then @ODBC_NUMERIC
  9000.                 when @DBTYPE_GUID           then @ODBC_GUID
  9001.                 when @DBTYPE_BYTES then
  9002.                     case
  9003.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  9004.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9005.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9006.                                  p.COLUMN_SIZE = 8
  9007.                             then @ODBC_TIMESTAMP
  9008.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9009.                             then @ODBC_BINARY
  9010.                         else @ODBC_VARBINARY
  9011.                     end
  9012.                 when @DBTYPE_STR then
  9013.                     case
  9014.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  9015.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  9016.                         else @ODBC_VARCHAR
  9017.                     end
  9018.                 when @DBTYPE_WSTR then
  9019.                     case
  9020.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  9021.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  9022.                         else @ODBC_NVARCHAR
  9023.                     end
  9024.                 when @DBTYPE_BSTR then
  9025.                     case
  9026.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  9027.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  9028.                         else @ODBC_NVARCHAR
  9029.                     end
  9030.                 else @ODBC_NVARCHAR     /*  Unknown OleDB datatype */
  9031.             end,
  9032.             TYPE_NAME = p.TYPE_NAME,
  9033.             COLUMN_SIZE =
  9034.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  9035.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9036.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  9037.             NUM_PREC_RADIX = NULL,
  9038.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  9039.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  9040.             COLUMN_DEF = c.COLUMN_DEFAULT,
  9041.             SQL_DATA_TYPE = NULL,
  9042.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  9043.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9044.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  9045.             IS_NULLABLE =
  9046.             case c.IS_NULLABLE
  9047.                 when 1 then 'YES'
  9048.                 else 'NO'
  9049.             end,
  9050.             SS_DATA_TYPE = NULL,
  9051.             COLUMN_FLAGS = c.COLUMN_FLAGS
  9052.         from master.dbo.SYSREMOTE_COLUMNS <
  9053.                     @table_server,
  9054.                     @table_catalog,
  9055.                     NULL,
  9056.                     @table_name,
  9057.                     NULL > c,
  9058.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  9059.                     @table_server > p
  9060.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  9061.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  9062.                 (TABLE_SCHEMA like @table_schema
  9063.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  9064.                 and (c.COLUMN_NAME like @column_name
  9065.                 or   @column_name is NULL)
  9066.     end
  9067.     else
  9068.     begin   /*  If wild carding */
  9069.         insert into #tmp_columns
  9070.         select
  9071.             TABLE_CAT = c.TABLE_CATALOG,
  9072.             TABLE_SCHEM = c.TABLE_SCHEMA,
  9073.             TABLE_NAME = c.TABLE_NAME,
  9074.             COLUMN_NAME = c.COLUMN_NAME,
  9075.             DATA_TYPE =
  9076.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  9077.                 when @DBTYPE_I2             then @ODBC_SMALLINT
  9078.                 when @DBTYPE_I4             then @ODBC_INT
  9079.                 when @DBTYPE_R4             then @ODBC_REAL
  9080.                 when @DBTYPE_R8             then @ODBC_FLOAT
  9081.                 when @DBTYPE_CY             then @ODBC_MONEY
  9082.                 when @DBTYPE_DATE           then @ODBC_DATETIME
  9083.                 when @DBTYPE_DBDATE         then @ODBC_DATETIME
  9084.                 when @DBTYPE_DBTIME         then @ODBC_DATETIME
  9085.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  9086.                 when @DBTYPE_BOOL           then @ODBC_BIT
  9087.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  9088.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  9089.                 when @DBTYPE_I1             then @ODBC_NUMERIC
  9090.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  9091.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  9092.                 when @DBTYPE_I8             then @ODBC_NUMERIC
  9093.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  9094.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  9095.                 when @DBTYPE_VARNUMERIC     then @ODBC_NUMERIC
  9096.                 when @DBTYPE_GUID           then @ODBC_GUID
  9097.                 when @DBTYPE_BYTES then
  9098.                     case
  9099.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  9100.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9101.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9102.                                  p.COLUMN_SIZE = 8
  9103.                             then @ODBC_TIMESTAMP
  9104.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9105.                             then @ODBC_BINARY
  9106.                         else @ODBC_VARBINARY
  9107.                     end
  9108.                 when @DBTYPE_STR then
  9109.                     case
  9110.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  9111.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  9112.                         else @ODBC_VARCHAR
  9113.                     end
  9114.                 when @DBTYPE_WSTR then
  9115.                     case
  9116.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  9117.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  9118.                         else @ODBC_NVARCHAR
  9119.                     end
  9120.                 when @DBTYPE_BSTR then
  9121.                     case
  9122.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  9123.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  9124.                         else @ODBC_NVARCHAR
  9125.                     end
  9126.                 else @ODBC_NVARCHAR     /*  Unknown OleDB datatype */
  9127.             end,
  9128.             TYPE_NAME = p.TYPE_NAME,
  9129.             COLUMN_SIZE =
  9130.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  9131.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9132.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  9133.             NUM_PREC_RADIX = NULL,
  9134.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  9135.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  9136.             COLUMN_DEF = c.COLUMN_DEFAULT,
  9137.             SQL_DATA_TYPE = NULL,
  9138.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  9139.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9140.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  9141.             IS_NULLABLE =
  9142.             case c.IS_NULLABLE
  9143.                 when 1 then 'YES'
  9144.                 else 'NO'
  9145.             end,
  9146.             SS_DATA_TYPE = NULL,
  9147.             COLUMN_FLAGS = c.COLUMN_FLAGS
  9148.         from master.dbo.SYSREMOTE_COLUMNS <
  9149.                     @table_server,
  9150.                     @table_catalog,
  9151.                     NULL,
  9152.                     NULL,
  9153.                     NULL > c,
  9154.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  9155.                     @table_server > p
  9156.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  9157.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  9158.                 (TABLE_SCHEMA like @table_schema
  9159.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  9160.                 and (c.COLUMN_NAME like @column_name
  9161.                 or   @column_name is NULL)
  9162.                 and c.TABLE_NAME like @table_name
  9163.     end
  9164.  
  9165.     update #tmp_columns
  9166.     set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
  9167.         SQL_DATETIME_SUB =
  9168.         case
  9169.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME then 3
  9170.             else #tmp_columns.SQL_DATETIME_SUB
  9171.         end,
  9172.         TYPE_NAME = 
  9173.         case 
  9174.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9175.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9176.                     BUFFER_LENGTH = 8
  9177.                 then 'timestamp'
  9178.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  9179.                     #tmp_columns.SQL_DATETIME_SUB = 0
  9180.                 then 'smalldatetime'
  9181.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and
  9182.                     #tmp_columns.COLUMN_SIZE = 10
  9183.                 then 'smallmoney'
  9184.             when (#tmp_columns.DATA_TYPE in (@ODBC_BINARY, @ODBC_CHAR) and
  9185.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH) or
  9186.                  #tmp_columns.DATA_TYPE in (@ODBC_TEXT, @ODBC_NTEXT, @ODBC_IMAGE)
  9187.                 then spt_dt.TYPE_NAME collate database_default
  9188.             else isnull(#tmp_columns.TYPE_NAME, spt_dt.TYPE_NAME collate database_default)
  9189.         end,
  9190.         COLUMN_SIZE =
  9191.         case
  9192.             when #tmp_columns.DATA_TYPE IN (@ODBC_REAL, @ODBC_FLOAT) then /* app. numeric types */
  9193.                 spt_dt.data_precision
  9194.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  9195.                     #tmp_columns.SQL_DATETIME_SUB = 0
  9196.                 then 16
  9197.             else #tmp_columns.COLUMN_SIZE
  9198.         end,
  9199.         BUFFER_LENGTH =
  9200.         case
  9201.             when #tmp_columns.DATA_TYPE IN (@ODBC_NUMERIC, @ODBC_DECIMAL) then  /* decimal/numeric types */
  9202.                 COLUMN_SIZE+2
  9203.             when isnull (#tmp_columns.BUFFER_LENGTH,0) = 0 then spt_dt.length
  9204.             else #tmp_columns.BUFFER_LENGTH
  9205.         end,
  9206.         DECIMAL_DIGITS =
  9207.         case
  9208.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME
  9209.                 then #tmp_columns.SQL_DATETIME_SUB
  9210.             else COALESCE (DECIMAL_DIGITS, spt_dt.numeric_scale)
  9211.         end,
  9212.         NUM_PREC_RADIX = spt_dt.RADIX,
  9213.         SS_DATA_TYPE = 
  9214.         case
  9215.             when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
  9216.                 and #tmp_columns.NULLABLE = 1
  9217.                     then @ODS_INT_NULL
  9218.             when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
  9219.                 then @ODS_TINYINT_NOT_NULL
  9220.             when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT    and #tmp_columns.NULLABLE = 0
  9221.                 then @ODS_SMALLINT_NOT_NULL
  9222.             when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
  9223.                 then @ODS_INT_NOT_NULL
  9224.             when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
  9225.                 then @ODS_FLOAT_NULL
  9226.             when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
  9227.                 then @ODS_REAL_NOT_NULL
  9228.             when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
  9229.                 then @ODS_FLOAT_NOT_NULL
  9230.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
  9231.                 then @ODS_MONEY_NULL
  9232.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
  9233.                 then @ODS_MONEY_NOT_NULL
  9234.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
  9235.                 then @ODS_DATETIME_NULL
  9236.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
  9237.                 then @ODS_DATETIME_NOT_NULL
  9238.             when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
  9239.             when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
  9240.             when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL  
  9241.             when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
  9242.             when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
  9243.             when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
  9244.             when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
  9245.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9246.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9247.                     BUFFER_LENGTH = 8
  9248.                 then @ODS_TIMESTAMP
  9249.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =  1
  9250.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9251.                 then  @ODS_BINARY_NULL
  9252.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =  0
  9253.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9254.                 then @ODS_BINARY_NOT_NULL
  9255.             when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
  9256.                 then @ODS_VARBINARY 
  9257.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
  9258.                 then @ODS_CHAR_NULL
  9259.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
  9260.                 then @ODS_CHAR_NOT_NULL
  9261.             when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
  9262.             else null
  9263.         end
  9264.         from master.dbo.spt_datatype_info spt_dt
  9265.         where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
  9266.             AND (spt_dt.ODBCVer is null or spt_dt.ODBCVer = @ODBCVer)
  9267.             and isnull(spt_dt.AUTO_INCREMENT,0) = 0
  9268.             and not spt_dt.TYPE_NAME = 'smalldatetime'
  9269.  
  9270.     select
  9271.             TABLE_CAT,          TABLE_SCHEM,        TABLE_NAME,
  9272.             COLUMN_NAME,        DATA_TYPE,          TYPE_NAME,
  9273.             COLUMN_SIZE,        BUFFER_LENGTH,      DECIMAL_DIGITS,
  9274.             NUM_PREC_RADIX,     NULLABLE,           REMARKS,
  9275.             COLUMN_DEF,         SQL_DATA_TYPE,      SQL_DATETIME_SUB,
  9276.             CHAR_OCTET_LENGTH,  ORDINAL_POSITION,   IS_NULLABLE,
  9277.             SS_DATA_TYPE
  9278.         from #tmp_columns
  9279.         order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  9280.  
  9281.         drop table #tmp_columns
  9282.  
  9283. go
  9284. grant execute on sp_columns_ex to public
  9285. go
  9286.  
  9287.  
  9288. if object_id('sp_table_privileges_ex', 'P') is not null
  9289.     drop proc sp_table_privileges_ex
  9290. go
  9291. raiserror(15339,-1,-1,'sp_table_privileges_ex')
  9292. go
  9293. create procedure sp_table_privileges_ex(
  9294.     @table_server       sysname,
  9295.     @table_name         sysname = null,
  9296.     @table_schema       sysname = null,
  9297.     @table_catalog      sysname = null)
  9298. as
  9299.     select
  9300.         TABLE_CAT = TABLE_CATALOG,
  9301.         TABLE_SCHEM = TABLE_SCHEMA,
  9302.         TABLE_NAME = TABLE_NAME,
  9303.         GRANTOR = GRANTOR,
  9304.         GRANTEE = GRANTEE,
  9305.         PRIVILEGE = PRIVILEGE_TYPE,
  9306.         IS_GRANTABLE = 
  9307.         case IS_GRANTABLE
  9308.             when 1 then 'YES'
  9309.             when 0 then 'NO'
  9310.             else null
  9311.         end
  9312.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  9313.                 @table_server,
  9314.                 @table_catalog,
  9315.                 NULL,
  9316.                 NULL >
  9317.     where (TABLE_SCHEMA like @table_schema
  9318.         or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  9319.         and (TABLE_NAME like @table_name
  9320.         or   @table_name is NULL)
  9321.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, PRIVILEGE
  9322. go
  9323. grant execute on sp_table_privileges_ex to public
  9324. go
  9325.  
  9326.  
  9327. if object_id('sp_column_privileges_ex', 'P') is not null
  9328.     drop proc sp_column_privileges_ex
  9329. go
  9330. raiserror(15339,-1,-1,'sp_column_privileges_ex')
  9331. go
  9332. create procedure sp_column_privileges_ex(
  9333.     @table_server       sysname,
  9334.     @table_name         sysname = null,
  9335.     @table_schema       sysname = null,
  9336.     @table_catalog      sysname = null,
  9337.     @column_name        sysname = null)
  9338. as
  9339.     select
  9340.         TABLE_CAT = TABLE_CATALOG,
  9341.         TABLE_SCHEM = TABLE_SCHEMA,
  9342.         TABLE_NAME = TABLE_NAME,
  9343.         COLUMN_NAME = COLUMN_NAME,
  9344.         GRANTOR = GRANTOR,
  9345.         GRANTEE = GRANTEE,
  9346.         PRIVILEGE = PRIVILEGE_TYPE,
  9347.         IS_GRANTABLE = 
  9348.         case IS_GRANTABLE
  9349.             when 1 then 'YES'
  9350.             when 0 then 'NO'
  9351.             else null
  9352.         end
  9353.     from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  9354.         @table_server,
  9355.         @table_catalog,
  9356.         @table_schema,
  9357.         @table_name,
  9358.         NULL >
  9359.     where (COLUMN_NAME like @column_name
  9360.         or @column_name is NULL)
  9361.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, PRIVILEGE
  9362. go
  9363. grant execute on sp_column_privileges_ex to public
  9364. go
  9365.  
  9366.  
  9367. if object_id('sp_indexes', 'P') is not null
  9368.     drop proc sp_indexes
  9369. go
  9370. raiserror(15339,-1,-1,'sp_indexes')
  9371. go
  9372. create procedure sp_indexes(
  9373.     @table_server       sysname,
  9374.     @table_name         sysname = null,
  9375.     @table_schema       sysname = null,
  9376.     @table_catalog      sysname = null,
  9377.     @index_name         sysname = null,
  9378.     @is_unique          bit = null)
  9379. as
  9380.     select
  9381.         TABLE_CAT = TABLE_CATALOG,
  9382.         TABLE_SCHEM = TABLE_SCHEMA,
  9383.         TABLE_NAME = TABLE_NAME,
  9384.             NON_UNIQUE = convert(smallint, 1 - [UNIQUE]),
  9385.         INDEX_QUALIFIER = TABLE_NAME,
  9386.         INDEX_NAME = INDEX_NAME,
  9387.             TYPE =
  9388.         case [CLUSTERED]
  9389.             when 1 then 1
  9390.             else 3
  9391.         end,
  9392.         ORDINAL_POSITION = ORDINAL_POSITION,
  9393.             COLUMN_NAME = COLUMN_NAME,
  9394.         ASC_OR_DESC = 
  9395.         case [COLLATION]
  9396.             when 1 then 'A'
  9397.             when 2 then 'D'
  9398.             else null
  9399.         end,
  9400.             CARDINALITY = CARDINALITY,
  9401.         PAGES = 
  9402.         case [CLUSTERED]
  9403.             when 1 then PAGES
  9404.             else NULL
  9405.         end,
  9406.             FILTER_CONDITION = FILTER_CONDITION
  9407.     from master.dbo.SYSREMOTE_INDEXES <
  9408.                 @table_server,
  9409.                 @table_catalog,
  9410.                 @table_schema,
  9411.                         @index_name,
  9412.                 NULL,           /* TYPE (index type) */
  9413.                 @table_name >
  9414.     where @is_unique is null or @is_unique = [UNIQUE]
  9415.     order by NON_UNIQUE, TYPE, INDEX_QUALIFIER, INDEX_NAME, ORDINAL_POSITION
  9416. go
  9417. grant execute on sp_indexes to public
  9418. go
  9419.  
  9420.  
  9421. if object_id('sp_foreignkeys', 'P') is not null
  9422.     drop proc sp_foreignkeys
  9423. go
  9424. raiserror(15339,-1,-1,'sp_foreignkeys')
  9425. go
  9426. create procedure sp_foreignkeys(
  9427.     @table_server       sysname,
  9428.     @pktab_name         sysname = null,
  9429.     @pktab_schema       sysname = null,
  9430.     @pktab_catalog      sysname = null,
  9431.     @fktab_name         sysname = null,
  9432.     @fktab_schema       sysname = null,
  9433.     @fktab_catalog      sysname = null)
  9434. as
  9435.     select
  9436.         PKTABLE_CAT = PK_TABLE_CATALOG,
  9437.         PKTABLE_SCHEM = PK_TABLE_SCHEMA,
  9438.         PKTABLE_NAME = PK_TABLE_NAME,
  9439.         PKCOLUMN_NAME = PK_COLUMN_NAME,
  9440.         FKTABLE_CAT = FK_TABLE_CATALOG,
  9441.         FKTABLE_SCHEM = FK_TABLE_SCHEMA,
  9442.         FKTABLE_NAME = FK_TABLE_NAME,
  9443.         FKCOLUMN_NAME = FK_COLUMN_NAME,
  9444.         KEY_SEQ = ORDINAL,
  9445.         UPDATE_RULE = 
  9446.         case UPDATE_RULE
  9447.             when 'CASCADE' then 0
  9448.             when 'NO ACTION' then 1
  9449.             when 'SET NULL' then 2
  9450.             else null
  9451.         end,
  9452.         DELETE_RULE = 
  9453.         case DELETE_RULE
  9454.             when 'CASCADE' then 0
  9455.             when 'NO ACTION' then 1
  9456.             when 'SET NULL' then 2
  9457.             else null
  9458.         end,
  9459.         FK_NAME = convert(sysname, NULL),
  9460.         PK_NAME = convert(sysname, NULL),
  9461.         DEFERRABILITY = convert(smallint, null)
  9462.  
  9463.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  9464.                 @table_server,
  9465.                 @pktab_catalog,
  9466.                 @pktab_schema,
  9467.                 @pktab_name,
  9468.                 @fktab_catalog,
  9469.                 @fktab_schema,
  9470.                 @fktab_name >
  9471.     order by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ
  9472. go
  9473. grant execute on sp_foreignkeys to public
  9474. go
  9475.  
  9476.  
  9477. if object_id('sp_primarykeys', 'P') is not null
  9478.     drop proc sp_primarykeys
  9479. go
  9480. raiserror(15339,-1,-1,'sp_primarykeys')
  9481. go
  9482. create procedure sp_primarykeys(
  9483.     @table_server       sysname,
  9484.     @table_name         sysname = null,
  9485.     @table_schema       sysname = null,
  9486.     @table_catalog      sysname = null)
  9487. as
  9488.     select
  9489.         TABLE_CAT = TABLE_CATALOG,
  9490.         TABLE_SCHEM = TABLE_SCHEMA,
  9491.         TABLE_NAME = TABLE_NAME,
  9492.         COLUMN_NAME = COLUMN_NAME,
  9493.         KEY_SEQ = ORDINAL,
  9494.         PK_NAME = convert(sysname, NULL)
  9495.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  9496.                 @table_server,
  9497.                 @table_catalog,
  9498.                 @table_schema,
  9499.                 @table_name >
  9500.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, KEY_SEQ
  9501. go
  9502. grant execute on sp_primarykeys to public
  9503. go
  9504.  
  9505. /*-------------- END CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  9506.  
  9507.  
  9508. dump tran master with no_log
  9509. go
  9510.  
  9511. if (charindex('6.50', @@version) = 0 and
  9512.     charindex('7.00', @@version) = 0 and
  9513.     charindex('8.00', @@version) = 0)
  9514. begin
  9515.     print ''
  9516.     print ''
  9517.     print 'Warning:'
  9518.     print 'you are installing the stored procedures '
  9519.     print 'on a pre 6.50 SQL Server.'
  9520.     print 'Ignore the following errors.'
  9521. end
  9522.  
  9523. print 'creating sp_ddopen'
  9524. go
  9525.  
  9526. /*  Procedure for pre-6.50 server */
  9527. create procedure sp_ddopen(
  9528.                @handle          int output,
  9529.                @procname        sysname,
  9530.                @scrollopt       int output,
  9531.                @ccopt           int output,
  9532.                @rows            int output,
  9533.                @p1              varchar(255) = null,
  9534.                @p2              varchar(255) = null,
  9535.                @p3              varchar(255) = null,
  9536.                @p4              varchar(255) = null,
  9537.                @p5              varchar(255) = null,
  9538.                @p6              varchar(255) = null,
  9539.                @p7              int = null,
  9540.                @ODBCVer         int = 2)
  9541. as
  9542.     set nocount on
  9543.     declare @ret int
  9544.  
  9545.     if @procname = 'sp_column_privileges'
  9546.     begin
  9547.         create table #spcolpriv (
  9548.             TABLE_QUALIFIER varchar(32) null,
  9549.             TABLE_OWNER varchar(32) null,
  9550.             TABLE_NAME varchar(32)  not null,
  9551.             COLUMN_NAME varchar(32) not null,
  9552.             GRANTOR varchar(32) null,
  9553.             GRANTEE varchar(32) not null,
  9554.             PRIVILEGE varchar(32) not null,
  9555.             IS_GRANTABLE varchar(3) null
  9556.             )
  9557.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  9558.         exec @ret = sp_cursoropen @handle output,
  9559.             'select * from #spcolpriv',
  9560.             @scrollopt output, @ccopt output, @rows output
  9561.         drop table #spcolpriv
  9562.     end
  9563.     else if @procname = 'sp_columns'
  9564.     begin
  9565.         create table #spcolumns (
  9566.             TABLE_QUALIFIER varchar(32) null,
  9567.             TABLE_OWNER varchar(32) null,
  9568.             TABLE_NAME varchar(32)  not null,
  9569.             COLUMN_NAME varchar(32) not null,
  9570.             DATA_TYPE smallint not null,
  9571.             TYPE_NAME varchar(32) not null,
  9572.             "PRECISION" int null,
  9573.             LENGTH int null,
  9574.             SCALE smallint null,
  9575.             RADIX smallint null,
  9576.             NULLABLE smallint not null,
  9577.             REMARKS varchar(254) null,
  9578.             COLUMN_DEF varchar(255) null,
  9579.             SQL_DATA_TYPE smallint not null,
  9580.             SQL_DATETIME_SUB smallint null,
  9581.             CHAR_OCTET_LENGTH int null,
  9582.             ORDINAL_POSITION int not null,
  9583.             IS_NULLABLE varchar(254) null,
  9584.             SS_DATA_TYPE tinyint null
  9585.             )
  9586.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  9587.         exec @ret = sp_cursoropen @handle output,
  9588.             'select * from #spcolumns',
  9589.             @scrollopt output, @ccopt output, @rows output
  9590.         drop table #spcolumns
  9591.     end
  9592.     else if @procname = 'sp_datatype_info'
  9593.     begin
  9594.         create table #spdatatypeinfo (
  9595.             TYPE_NAME           varchar(32)  not null,
  9596.             DATA_TYPE           smallint not null,
  9597.             "PRECISION"         int null,
  9598.             LITERAL_PREFIX      varchar(32) null,
  9599.             LITERAL_SUFFIX      varchar(32)    null,
  9600.             CREATE_PARAMS       varchar(32)    null,
  9601.             NULLABLE            smallint   not null,
  9602.             CASE_SENSITIVE      smallint   not null,
  9603.             SEARCHABLE          smallint   not null,
  9604.             UNSIGNED_ATTRIBUTE  smallint   null,
  9605.             MONEY   smallint    not null,
  9606.             AUTO_INCREMENT      smallint    null,
  9607.             LOCAL_TYPE_NAME     varchar(32) null,
  9608.             MINIMUM_SCALE       smallint     null,
  9609.             MAXIMUM_SCALE       smallint   null,
  9610.             SQL_DATA_TYPE       smallint      not null,
  9611.             SQL_DATETIME_SUB    smallint   null,
  9612.             NUM_PREC_RADIX      int  null,
  9613.             INTERVAL_PRECISION  smallint    NULL,
  9614.             USERTYPE            smallint not null)
  9615.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  9616.         exec @ret = sp_cursoropen @handle output,
  9617.             'select * from #spdatatypeinfo',
  9618.             @scrollopt output, @ccopt output, @rows output
  9619.         drop table #spdatatypeinfo
  9620.     end
  9621.     else if @procname = 'sp_fkeys'
  9622.     begin
  9623.         create table #spfkeys (
  9624.             PKTABLE_QUALIFIER varchar(32)    null,
  9625.             PKTABLE_OWNER varchar(32)   null,
  9626.             PKTABLE_NAME varchar(32)  not null,
  9627.             PKCOLUMN_NAME varchar(32)   not null,
  9628.             FKTABLE_QUALIFIER varchar(32)   null,
  9629.             FKTABLE_OWNER varchar(32)   null,
  9630.             FKTABLE_NAME varchar(32)  not null,
  9631.             FKCOLUMN_NAME varchar(32)   not null,
  9632.             KEY_SEQ smallint not null,
  9633.             UPDATE_RULE smallint null,
  9634.             DELETE_RULE smallint null,
  9635.             FK_NAME varchar(32) null,
  9636.             PK_NAME varchar(32) null,
  9637.             DEFERRABILITY smallint null
  9638.             )
  9639.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  9640.         exec @ret = sp_cursoropen @handle output,
  9641.             'select * from #spfkeys',
  9642.             @scrollopt output, @ccopt output, @rows output
  9643.         drop table #spfkeys
  9644.     end
  9645.     else if @procname = 'sp_pkeys'
  9646.     begin
  9647.         create table #sppkeys (
  9648.             TABLE_QUALIFIER varchar(32)   null,
  9649.             TABLE_OWNER varchar(32)   null,
  9650.             TABLE_NAME varchar(32)  not null,
  9651.             COLUMN_NAME varchar(32)  not null,
  9652.             KEY_SEQ smallint not null,
  9653.             PK_NAME varchar(32) null
  9654.             )
  9655.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  9656.         exec @ret = sp_cursoropen @handle output,
  9657.             'select * from #sppkeys',
  9658.             @scrollopt output, @ccopt output, @rows output
  9659.         drop table #sppkeys
  9660.     end
  9661.     else if @procname = 'sp_special_columns'
  9662.     begin
  9663.         create table #spspeccol (
  9664.             SCOPE smallint null,
  9665.             COLUMN_NAME varchar(32) not null,
  9666.             DATA_TYPE smallint not null,
  9667.             TYPE_NAME varchar(32) not null,
  9668.             "PRECISION" int null,
  9669.             LENGTH int null,
  9670.             SCALE smallint null,
  9671.             PSEUDO_COLUMN smallint null
  9672.             )
  9673.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  9674.         exec @ret = sp_cursoropen @handle output,
  9675.             'select * from #spspeccol',
  9676.             @scrollopt output, @ccopt output, @rows output
  9677.         drop table #spspeccol
  9678.     end
  9679.     else if @procname = 'sp_sproc_columns'
  9680.     begin
  9681.         create table #spproccol (
  9682.             PROCEDURE_QUALIFIER varchar(32)  null,
  9683.             PROCEDURE_OWNER varchar(32)  null,
  9684.             PROCEDURE_NAME varchar(32)  not null,
  9685.             COLUMN_NAME varchar(32) not null,
  9686.             COLUMN_TYPE smallint not null,
  9687.             DATA_TYPE smallint not null,
  9688.             TYPE_NAME varchar(32) not null,
  9689.             "PRECISION" int null,
  9690.             LENGTH int null,
  9691.             SCALE smallint null,
  9692.             RADIX smallint null,
  9693.             NULLABLE smallint not null,
  9694.             REMARKS varchar(254) null,
  9695.             COLUMN_DEF varchar(255) null,
  9696.             SQL_DATA_TYPE smallint not null,
  9697.             SQL_DATETIME_SUB smallint null,
  9698.             CHAR_OCTET_LENGTH int null,
  9699.             ORDINAL_POSITION int not null,
  9700.             IS_NULLABLE varchar(254) null,
  9701.             SS_DATA_TYPE tinyint null
  9702.             )
  9703.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  9704.         exec @ret = sp_cursoropen @handle output,
  9705.             'select * from #spproccol',
  9706.             @scrollopt output, @ccopt output, @rows output
  9707.         drop table #spproccol
  9708.     end
  9709.     else if @procname = 'sp_statistics'
  9710.     begin
  9711.         create table #spstatistics (
  9712.             TABLE_QUALIFIER varchar(32)   null,
  9713.             TABLE_OWNER varchar(32)   null,
  9714.             TABLE_NAME varchar(32)  not null,
  9715.             NON_UNIQUE smallint null,
  9716.             INDEX_QUALIFIER varchar(32) null,
  9717.             INDEX_NAME varchar(32)  null,
  9718.             TYPE smallint not null,
  9719.             SEQ_IN_INDEX smallint null,
  9720.             COLUMN_NAME varchar(32) null,
  9721.             COLLATION char(1) null,
  9722.             CARDINALITY int null,
  9723.             PAGES int null,
  9724.             FILTER_CONDITION varchar(128) null
  9725.             )
  9726.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  9727.         exec @ret = sp_cursoropen @handle output,
  9728.             'select * from #spstatistics',
  9729.             @scrollopt output, @ccopt output, @rows output
  9730.         drop table #spstatistics
  9731.     end
  9732.     else if @procname = 'sp_stored_procedures'
  9733.     begin
  9734.         create table #spprocedures (
  9735.             PROCEDURE_QUALIFIER varchar(32)  null,
  9736.             PROCEDURE_OWNER varchar(32)  null,
  9737.             PROCEDURE_NAME varchar(32)  not null,
  9738.             NUM_INPUT_PARAMS int null,
  9739.             NUM_OUTPUT_PARAMS int null,
  9740.             NUM_RESULT_SETS int null,
  9741.             REMARKS varchar(254) null,
  9742.             PROCEDURE_TYPE smallint null
  9743.             )
  9744.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  9745.         exec @ret = sp_cursoropen @handle output,
  9746.             'select * from #spprocedures',
  9747.             @scrollopt output, @ccopt output, @rows output
  9748.         drop table #spprocedures
  9749.     end
  9750.     else if @procname = 'sp_table_privileges'
  9751.     begin
  9752.         create table #sptabpriv (
  9753.             TABLE_QUALIFIER varchar(32) null,
  9754.             TABLE_OWNER varchar(32) null,
  9755.             TABLE_NAME varchar(32)  not null,
  9756.             GRANTOR varchar(32) null,
  9757.             GRANTEE varchar(32) not null,
  9758.             PRIVILEGE varchar(32) not null,
  9759.             IS_GRANTABLE varchar(3) null
  9760.             )
  9761.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  9762.         exec @ret = sp_cursoropen @handle output,
  9763.             'select * from #sptabpriv',
  9764.             @scrollopt output, @ccopt output, @rows output
  9765.         drop table #sptabpriv
  9766.     end
  9767.     else if @procname = 'sp_tables'
  9768.     begin
  9769.         create table #sptables (
  9770.             TABLE_QUALIFIER varchar(32) null,
  9771.             TABLE_OWNER varchar(32) null,
  9772.             TABLE_NAME varchar(32)  null,
  9773.             TABLE_TYPE   varchar(32) null,
  9774.             REMARKS varchar(254) null)
  9775.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  9776.         exec @ret = sp_cursoropen @handle output,
  9777.             'select * from #sptables',
  9778.             @scrollopt output, @ccopt output, @rows output
  9779.         drop table #sptables
  9780.     end
  9781.     else
  9782.         print 'Unknown sp_ddopen procedure'
  9783.     select @ret = isnull(@ret,0)
  9784.     return isnull(@ret,0)
  9785. go
  9786.  
  9787. if (charindex('7.00', @@version) > 0 or
  9788.     charindex('8.00', @@version) > 0)
  9789.     drop procedure sp_ddopen
  9790. else
  9791. begin
  9792.     print ''
  9793.     print ''
  9794.     print 'Warning:'
  9795.     print 'you are installing the stored procedures '
  9796.     print 'on a pre 7.0 SQL Server.'
  9797.     print 'Ignore the following errors.'
  9798. end
  9799. go
  9800.  
  9801. /*  Procedure for 7.0 server */
  9802. create procedure sp_ddopen; 1(
  9803.                @handle          int output,
  9804.                @procname        sysname,
  9805.                @scrollopt       int output,
  9806.                @ccopt           int output,
  9807.                @rows            int output,
  9808.                @p1              nvarchar(774) = null,
  9809.                @p2              nvarchar(774) = null,
  9810.                @p3              nvarchar(774) = null,
  9811.                @p4              nvarchar(774) = null,
  9812.                @p5              nvarchar(774) = null,
  9813.                @p6              nvarchar(774) = null,
  9814.                @p7              int = null,
  9815.                @ODBCVer         int = 2)
  9816. as
  9817.     set nocount on
  9818.     declare @ret int
  9819.  
  9820.     if @procname = 'sp_column_privileges'
  9821.     begin
  9822.         exec @ret = sp_ddopen;2 @handle output, 
  9823.                             @scrollopt output, 
  9824.                             @ccopt output, 
  9825.                             @rows output, 
  9826.                             @p1,
  9827.                             @p2,
  9828.                             @p3,
  9829.                             @p4
  9830.     end
  9831.     else if @procname = 'sp_columns' or @procname = 'sp_columns_ex'
  9832.     begin
  9833.         exec @ret = sp_ddopen;3 @handle output,
  9834.                             @procname,
  9835.                             @scrollopt output, 
  9836.                             @ccopt output, 
  9837.                             @rows output, 
  9838.                             @p1,
  9839.                             @p2,
  9840.                             @p3,
  9841.                             @p4,
  9842.                             @p5,
  9843.                             @ODBCVer
  9844.     end
  9845.     else if @procname = 'sp_datatype_info'
  9846.     begin
  9847.         exec @ret = sp_ddopen;4 @handle output,
  9848.                             @scrollopt output, 
  9849.                             @ccopt output, 
  9850.                             @rows output, 
  9851.                             @p7,
  9852.                             @ODBCVer
  9853.     end
  9854.     else if @procname = 'sp_fkeys'
  9855.     begin
  9856.         exec @ret = sp_ddopen;5 @handle output, 
  9857.                             @scrollopt output, 
  9858.                             @ccopt output, 
  9859.                             @rows output, 
  9860.                             @p1,
  9861.                             @p2,
  9862.                             @p3,
  9863.                             @p4,
  9864.                             @p5,
  9865.                             @p6
  9866.     end
  9867.     else if @procname = 'sp_pkeys'
  9868.     begin
  9869.         exec @ret = sp_ddopen;6 @handle output, 
  9870.                             @scrollopt output, 
  9871.                             @ccopt output, 
  9872.                             @rows output, 
  9873.                             @p1,
  9874.                             @p2,
  9875.                             @p3
  9876.     end
  9877.     else if @procname = 'sp_special_columns'
  9878.     begin
  9879.         exec @ret = sp_ddopen;7 @handle output, 
  9880.                             @scrollopt output, 
  9881.                             @ccopt output, 
  9882.                             @rows output, 
  9883.                             @p1,
  9884.                             @p2,
  9885.                             @p3,
  9886.                             @p4,
  9887.                             @p5,
  9888.                             @p6,
  9889.                             @ODBCVer
  9890.     end
  9891.     else if @procname = 'sp_sproc_columns'
  9892.     begin
  9893.         exec @ret = sp_ddopen;8 @handle output, 
  9894.                             @scrollopt output, 
  9895.                             @ccopt output, 
  9896.                             @rows output, 
  9897.                             @p1,
  9898.                             @p2,
  9899.                             @p3,
  9900.                             @p4,
  9901.                             @ODBCVer
  9902.     end
  9903.     else if @procname = 'sp_statistics'
  9904.     begin
  9905.         exec @ret = sp_ddopen;9 @handle output, 
  9906.                             @scrollopt output, 
  9907.                             @ccopt output, 
  9908.                             @rows output, 
  9909.                             @p1,
  9910.                             @p2,
  9911.                             @p3,
  9912.                             @p4,
  9913.                             @p5,
  9914.                             @p6
  9915.     end
  9916.     else if @procname = 'sp_stored_procedures'
  9917.     begin
  9918.         exec @ret = sp_ddopen;10 @handle output, 
  9919.                              @scrollopt output, 
  9920.                              @ccopt output, 
  9921.                              @rows output, 
  9922.                              @p1,
  9923.                              @p2,
  9924.                              @p3
  9925.     end
  9926.     else if @procname = 'sp_table_privileges'
  9927.     begin
  9928.         exec @ret = sp_ddopen;11 @handle output, 
  9929.                              @scrollopt output, 
  9930.                              @ccopt output, 
  9931.                              @rows output, 
  9932.                              @p1,
  9933.                              @p2,
  9934.                              @p3
  9935.     end
  9936.     else if @procname = 'sp_tables' or @procname = 'sp_tables_ex'
  9937.     begin
  9938.         exec @ret = sp_ddopen;12 @handle output,
  9939.                             @procname,
  9940.                             @scrollopt output, 
  9941.                             @ccopt output, 
  9942.                             @rows output, 
  9943.                             @p1,
  9944.                             @p2,
  9945.                             @p3,
  9946.                             @p4,
  9947.                             @p5
  9948.     end
  9949.     else if @procname = 'sp_tableswc'
  9950.     begin
  9951.         exec @ret = sp_ddopen;13 @handle output,
  9952.                             @procname,
  9953.                             @scrollopt output,
  9954.                             @ccopt output,
  9955.                             @rows output,
  9956.                             @p1,
  9957.                             @p2,
  9958.                             @p3,
  9959.                             @p4
  9960.     end
  9961.     else
  9962.         print 'Unknown sp_ddopen procedure'
  9963.     select @ret = isnull(@ret,0)
  9964.     return isnull(@ret,0)
  9965. go
  9966.  
  9967.  
  9968. if (charindex('7.00', @@version) = 0 and
  9969.     charindex('8.00', @@version) = 0)
  9970. begin
  9971.     print ''
  9972.     print ''
  9973.     print 'Warning:'
  9974.     print 'you are installing the stored procedures '
  9975.     print 'on a pre 7.0 SQL Server.'
  9976.     print 'Ignore the following errors.'
  9977. end
  9978. go
  9979.  
  9980. create procedure sp_ddopen; 2(
  9981.                @handle          int output,
  9982.                @scrollopt       int output,
  9983.                @ccopt           int output,
  9984.                @rows            int output,
  9985.                @p1              nvarchar(774),
  9986.                @p2              nvarchar(774),
  9987.                @p3              nvarchar(774),
  9988.                @p4              nvarchar(774))
  9989. as
  9990.     set nocount on
  9991.     declare @ret int
  9992.  
  9993.     create table #spcolpriv (
  9994.         TABLE_QUALIFIER sysname null,
  9995.         TABLE_OWNER sysname null,
  9996.         TABLE_NAME sysname not null,
  9997.         COLUMN_NAME sysname not null,
  9998.         GRANTOR sysname null,
  9999.         GRANTEE sysname not null,
  10000.         PRIVILEGE varchar(32) not null,
  10001.         IS_GRANTABLE varchar(3) null
  10002.         )
  10003.     insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  10004.     exec @ret = sp_cursoropen @handle output,
  10005.         'select * from #spcolpriv',
  10006.         @scrollopt output, @ccopt output, @rows output
  10007.     drop table #spcolpriv
  10008.     return @ret
  10009. go
  10010.  
  10011.  
  10012. if (charindex('7.00', @@version) = 0 and
  10013.     charindex('8.00', @@version) = 0)
  10014. begin
  10015.     print ''
  10016.     print ''
  10017.     print 'Warning:'
  10018.     print 'you are installing the stored procedures '
  10019.     print 'on a pre 7.0 SQL Server.'
  10020.     print 'Ignore the following errors.'
  10021. end
  10022. go
  10023.  
  10024. create procedure sp_ddopen; 3(
  10025.                @handle          int output,
  10026.                @procname        sysname,
  10027.                @scrollopt       int output,
  10028.                @ccopt           int output,
  10029.                @rows            int output,
  10030.                @p1              nvarchar(774),
  10031.                @p2              nvarchar(774),
  10032.                @p3              nvarchar(774),
  10033.                @p4              nvarchar(774),
  10034.                @p5              nvarchar(774),
  10035.                @ODBCVer         int)
  10036. as
  10037.     set nocount on
  10038.     declare @ret int
  10039.  
  10040.     create table #spcolumns (
  10041.         TABLE_QUALIFIER sysname null,
  10042.         TABLE_OWNER sysname null,
  10043.         TABLE_NAME sysname not null,
  10044.         COLUMN_NAME sysname not null,
  10045.         DATA_TYPE smallint not null,
  10046.         TYPE_NAME sysname not null,
  10047.         "PRECISION" int null,
  10048.         LENGTH int null,
  10049.         SCALE smallint null,
  10050.         RADIX smallint null,
  10051.         NULLABLE smallint not null,
  10052.         REMARKS varchar(254) null,
  10053.         COLUMN_DEF nvarchar(3000) null,
  10054.         SQL_DATA_TYPE smallint not null,
  10055.         SQL_DATETIME_SUB smallint null,
  10056.         CHAR_OCTET_LENGTH int null,
  10057.         ORDINAL_POSITION int not null,
  10058.         IS_NULLABLE varchar(254) null,
  10059.         SS_DATA_TYPE tinyint null
  10060.         )
  10061.  
  10062.     if @procname = 'sp_columns'
  10063.     begin
  10064.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  10065.     end
  10066.     else
  10067.     begin
  10068.         insert into #spcolumns exec sp_columns_ex @p1,@p2,@p3,@p4,@p5,@ODBCVer
  10069.     end
  10070.     exec @ret = sp_cursoropen @handle output,
  10071.         'select * from #spcolumns',
  10072.         @scrollopt output, @ccopt output, @rows output
  10073.     drop table #spcolumns
  10074.     return @ret
  10075. go
  10076.  
  10077.  
  10078.  
  10079. if (charindex('7.00', @@version) = 0 and
  10080.     charindex('8.00', @@version) = 0)
  10081. begin
  10082.     print ''
  10083.     print ''
  10084.     print 'Warning:'
  10085.     print 'you are installing the stored procedures '
  10086.     print 'on a pre 7.0 SQL Server.'
  10087.     print 'Ignore the following errors.'
  10088. end
  10089. go
  10090.  
  10091. create procedure sp_ddopen; 4(
  10092.                @handle          int output,
  10093.                @scrollopt       int output,
  10094.                @ccopt           int output,
  10095.                @rows            int output,
  10096.                @p7              int,
  10097.                @ODBCVer         int)
  10098. as
  10099.     set nocount on
  10100.     declare @ret int
  10101.  
  10102.     create table #spdatatypeinfo (
  10103.         TYPE_NAME           sysname  not null,
  10104.         DATA_TYPE           smallint not null,
  10105.         "PRECISION"         int null,
  10106.         LITERAL_PREFIX      varchar(32) null,
  10107.         LITERAL_SUFFIX      varchar(32)    null,
  10108.         CREATE_PARAMS       varchar(32)    null,
  10109.         NULLABLE            smallint   not null,
  10110.         CASE_SENSITIVE      smallint   not null,
  10111.         SEARCHABLE          smallint   not null,
  10112.         UNSIGNED_ATTRIBUTE  smallint   null,
  10113.         MONEY   smallint    not null,
  10114.         AUTO_INCREMENT      smallint    null,
  10115.         LOCAL_TYPE_NAME     sysname null,
  10116.         MINIMUM_SCALE       smallint     null,
  10117.         MAXIMUM_SCALE       smallint   null,
  10118.         SQL_DATA_TYPE       smallint      not null,
  10119.         SQL_DATETIME_SUB    smallint   null,
  10120.         NUM_PREC_RADIX      int  null,
  10121.         INTERVAL_PRECISION  smallint    NULL,
  10122.         USERTYPE            smallint not null)
  10123.  
  10124.     insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  10125.     exec @ret = sp_cursoropen @handle output,
  10126.         'select * from #spdatatypeinfo',
  10127.         @scrollopt output, @ccopt output, @rows output
  10128.     drop table #spdatatypeinfo
  10129.     return @ret
  10130. go
  10131.  
  10132.  
  10133.  
  10134. if (charindex('7.00', @@version) = 0 and
  10135.     charindex('8.00', @@version) = 0)
  10136. begin
  10137.     print ''
  10138.     print ''
  10139.     print 'Warning:'
  10140.     print 'you are installing the stored procedures '
  10141.     print 'on a pre 7.0 SQL Server.'
  10142.     print 'Ignore the following errors.'
  10143. end
  10144. go
  10145.  
  10146. create procedure sp_ddopen; 5(
  10147.                @handle          int output,
  10148.                @scrollopt       int output,
  10149.                @ccopt           int output,
  10150.                @rows            int output,
  10151.                @p1              nvarchar(774),
  10152.                @p2              nvarchar(774),
  10153.                @p3              nvarchar(774),
  10154.                @p4              nvarchar(774),
  10155.                @p5              nvarchar(774),
  10156.                @p6              nvarchar(774))
  10157. as
  10158.     set nocount on
  10159.     declare @ret int
  10160.  
  10161.     create table #spfkeys (
  10162.         PKTABLE_QUALIFIER sysname    null,
  10163.         PKTABLE_OWNER sysname   null,
  10164.         PKTABLE_NAME sysname  not null,
  10165.         PKCOLUMN_NAME sysname  not null,
  10166.         FKTABLE_QUALIFIER sysname   null,
  10167.         FKTABLE_OWNER sysname   null,
  10168.         FKTABLE_NAME sysname  not null,
  10169.         FKCOLUMN_NAME sysname  not null,
  10170.         KEY_SEQ smallint not null,
  10171.         UPDATE_RULE smallint null,
  10172.         DELETE_RULE smallint null,
  10173.         FK_NAME sysname null,
  10174.         PK_NAME sysname null,
  10175.         DEFERRABILITY smallint null
  10176.         )
  10177.     insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  10178.     exec @ret = sp_cursoropen @handle output,
  10179.         'select * from #spfkeys',
  10180.         @scrollopt output, @ccopt output, @rows output
  10181.     drop table #spfkeys
  10182.     return @ret
  10183. go
  10184.  
  10185.  
  10186.  
  10187. if (charindex('7.00', @@version) = 0 and
  10188.     charindex('8.00', @@version) = 0)
  10189. begin
  10190.     print ''
  10191.     print ''
  10192.     print 'Warning:'
  10193.     print 'you are installing the stored procedures '
  10194.     print 'on a pre 7.0 SQL Server.'
  10195.     print 'Ignore the following errors.'
  10196. end
  10197. go
  10198.  
  10199. create procedure sp_ddopen; 6(
  10200.                @handle          int output,
  10201.                @scrollopt       int output,
  10202.                @ccopt           int output,
  10203.                @rows            int output,
  10204.                @p1              nvarchar(774),
  10205.                @p2              nvarchar(774),
  10206.                @p3              nvarchar(774))
  10207. as
  10208.     set nocount on
  10209.     declare @ret int
  10210.  
  10211.     create table #sppkeys (
  10212.         TABLE_QUALIFIER sysname   null,
  10213.         TABLE_OWNER sysname   null,
  10214.         TABLE_NAME sysname  not null,
  10215.         COLUMN_NAME sysname  not null,
  10216.         KEY_SEQ smallint not null,
  10217.         PK_NAME sysname null
  10218.         )
  10219.     insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  10220.     exec @ret = sp_cursoropen @handle output,
  10221.         'select * from #sppkeys',
  10222.         @scrollopt output, @ccopt output, @rows output
  10223.     drop table #sppkeys
  10224.     return @ret
  10225. go
  10226.  
  10227.  
  10228.  
  10229. if (charindex('7.00', @@version) = 0 and
  10230.     charindex('8.00', @@version) = 0)
  10231. begin
  10232.     print ''
  10233.     print ''
  10234.     print 'Warning:'
  10235.     print 'you are installing the stored procedures '
  10236.     print 'on a pre 7.0 SQL Server.'
  10237.     print 'Ignore the following errors.'
  10238. end
  10239. go
  10240.  
  10241. create procedure sp_ddopen; 7(
  10242.                @handle          int output,
  10243.                @scrollopt       int output,
  10244.                @ccopt           int output,
  10245.                @rows            int output,
  10246.                @p1              nvarchar(774),
  10247.                @p2              nvarchar(774),
  10248.                @p3              nvarchar(774),
  10249.                @p4              nvarchar(774),
  10250.                @p5              nvarchar(774),
  10251.                @p6              nvarchar(774),
  10252.                @ODBCVer         int)
  10253. as
  10254.     set nocount on
  10255.     declare @ret int
  10256.  
  10257.     create table #spspeccol (
  10258.         SCOPE smallint null,
  10259.         COLUMN_NAME sysname not null,
  10260.         DATA_TYPE smallint not null,
  10261.         TYPE_NAME sysname not null,
  10262.         "PRECISION" int null,
  10263.         LENGTH int null,
  10264.         SCALE smallint null,
  10265.         PSEUDO_COLUMN smallint null
  10266.         )
  10267.     insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  10268.     exec @ret = sp_cursoropen @handle output,
  10269.         'select * from #spspeccol',
  10270.         @scrollopt output, @ccopt output, @rows output
  10271.     drop table #spspeccol
  10272.     return @ret
  10273. go
  10274.  
  10275.  
  10276.  
  10277. if (charindex('7.00', @@version) = 0 and
  10278.     charindex('8.00', @@version) = 0)
  10279. begin
  10280.     print ''
  10281.     print ''
  10282.     print 'Warning:'
  10283.     print 'you are installing the stored procedures '
  10284.     print 'on a pre 7.0 SQL Server.'
  10285.     print 'Ignore the following errors.'
  10286. end
  10287. go
  10288.  
  10289. create procedure sp_ddopen; 8(
  10290.                @handle          int output,
  10291.                @scrollopt       int output,
  10292.                @ccopt           int output,
  10293.                @rows            int output,
  10294.                @p1              nvarchar(774),
  10295.                @p2              nvarchar(774),
  10296.                @p3              nvarchar(774),
  10297.                @p4              nvarchar(774),
  10298.                @ODBCVer         int)
  10299. as
  10300.     set nocount on
  10301.     declare @ret int
  10302.  
  10303.     create table #spproccol (
  10304.         PROCEDURE_QUALIFIER sysname  null,
  10305.         PROCEDURE_OWNER sysname  null,
  10306.         PROCEDURE_NAME sysname not null,
  10307.         COLUMN_NAME sysname not null,
  10308.         COLUMN_TYPE smallint not null,
  10309.         DATA_TYPE smallint not null,
  10310.         TYPE_NAME sysname not null,
  10311.         "PRECISION" int null,
  10312.         LENGTH int null,
  10313.         SCALE smallint null,
  10314.         RADIX smallint null,
  10315.         NULLABLE smallint not null,
  10316.         REMARKS varchar(254) null,
  10317.         COLUMN_DEF nvarchar(3000) null,
  10318.         SQL_DATA_TYPE smallint not null,
  10319.         SQL_DATETIME_SUB smallint null,
  10320.         CHAR_OCTET_LENGTH int null,
  10321.         ORDINAL_POSITION int not null,
  10322.         IS_NULLABLE varchar(254) null,
  10323.         SS_DATA_TYPE tinyint null
  10324.         )
  10325.     insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  10326.     exec @ret = sp_cursoropen @handle output,
  10327.         'select * from #spproccol',
  10328.         @scrollopt output, @ccopt output, @rows output
  10329.     drop table #spproccol
  10330.     return @ret
  10331. go
  10332.  
  10333.  
  10334.  
  10335. if (charindex('7.00', @@version) = 0 and
  10336.     charindex('8.00', @@version) = 0)
  10337. begin
  10338.     print ''
  10339.     print ''
  10340.     print 'Warning:'
  10341.     print 'you are installing the stored procedures '
  10342.     print 'on a pre 7.0 SQL Server.'
  10343.     print 'Ignore the following errors.'
  10344. end
  10345. go
  10346.  
  10347. create procedure sp_ddopen; 9(
  10348.                @handle          int output,
  10349.                @scrollopt       int output,
  10350.                @ccopt           int output,
  10351.                @rows            int output,
  10352.                @p1              nvarchar(774),
  10353.                @p2              nvarchar(774),
  10354.                @p3              nvarchar(774),
  10355.                @p4              nvarchar(774),
  10356.                @p5              nvarchar(774),
  10357.                @p6              nvarchar(774))
  10358. as
  10359.     set nocount on
  10360.     declare @ret int
  10361.  
  10362.     create table #spstatistics (
  10363.         TABLE_QUALIFIER sysname   null,
  10364.         TABLE_OWNER sysname   null,
  10365.         TABLE_NAME sysname  not null,
  10366.         NON_UNIQUE smallint null,
  10367.         INDEX_QUALIFIER sysname null,
  10368.         INDEX_NAME sysname null,
  10369.         TYPE smallint not null,
  10370.         SEQ_IN_INDEX smallint null,
  10371.         COLUMN_NAME sysname null,
  10372.         COLLATION char(1) null,
  10373.         CARDINALITY int null,
  10374.         PAGES int null,
  10375.         FILTER_CONDITION varchar(128) null
  10376.         )
  10377.     insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  10378.     exec @ret = sp_cursoropen @handle output,
  10379.         'select * from #spstatistics',
  10380.         @scrollopt output, @ccopt output, @rows output
  10381.     drop table #spstatistics
  10382.     return @ret
  10383. go
  10384.                
  10385.  
  10386.  
  10387. if (charindex('7.00', @@version) = 0 and
  10388.     charindex('8.00', @@version) = 0)
  10389. begin
  10390.     print ''
  10391.     print ''
  10392.     print 'Warning:'
  10393.     print 'you are installing the stored procedures '
  10394.     print 'on a pre 7.0 SQL Server.'
  10395.     print 'Ignore the following errors.'
  10396. end
  10397. go
  10398.  
  10399. create procedure sp_ddopen; 10(
  10400.                @handle          int output,
  10401.                @scrollopt           int output,
  10402.                @ccopt           int output,
  10403.                @rows            int output,
  10404.                @p1              nvarchar(774),
  10405.                @p2              nvarchar(774),
  10406.                @p3              nvarchar(774))
  10407. as
  10408.     set nocount on
  10409.     declare @ret int
  10410.  
  10411.     create table #spprocedures (
  10412.         PROCEDURE_QUALIFIER sysname  null,
  10413.         PROCEDURE_OWNER sysname  null,
  10414.         PROCEDURE_NAME nvarchar(134) not null, /*134=sysname+';'+ltrim(str(c.number,5))*/
  10415.         NUM_INPUT_PARAMS int null,
  10416.         NUM_OUTPUT_PARAMS int null,
  10417.         NUM_RESULT_SETS int null,
  10418.         REMARKS varchar(254) null,
  10419.         PROCEDURE_TYPE smallint null
  10420.         )
  10421.     insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  10422.     exec @ret = sp_cursoropen @handle output,
  10423.         'select * from #spprocedures',
  10424.         @scrollopt output, @ccopt output, @rows output
  10425.     drop table #spprocedures
  10426.     return @ret
  10427. go
  10428.  
  10429.  
  10430.  
  10431. if (charindex('7.00', @@version) = 0 and
  10432.     charindex('8.00', @@version) = 0)
  10433. begin
  10434.     print ''
  10435.     print ''
  10436.     print 'Warning:'
  10437.     print 'you are installing the stored procedures '
  10438.     print 'on a pre 7.0 SQL Server.'
  10439.     print 'Ignore the following errors.'
  10440. end
  10441. go
  10442.  
  10443. create procedure sp_ddopen; 11(
  10444.                @handle          int output,
  10445.                @scrollopt       int output,
  10446.                @ccopt           int output,
  10447.                @rows            int output,
  10448.                @p1              nvarchar(774),
  10449.                @p2              nvarchar(774),
  10450.                @p3              nvarchar(774))
  10451. as
  10452.     set nocount on
  10453.     declare @ret int
  10454.  
  10455.     create table #sptabpriv (
  10456.         TABLE_QUALIFIER sysname null,
  10457.         TABLE_OWNER sysname null,
  10458.         TABLE_NAME sysname not null,
  10459.         GRANTOR sysname null,
  10460.         GRANTEE sysname not null,
  10461.         PRIVILEGE varchar(32) not null,
  10462.         IS_GRANTABLE varchar(3) null
  10463.         )
  10464.     insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  10465.     exec @ret = sp_cursoropen @handle output,
  10466.         'select * from #sptabpriv',
  10467.         @scrollopt output, @ccopt output, @rows output
  10468.     drop table #sptabpriv
  10469.     return @ret
  10470. go
  10471.  
  10472.  
  10473.  
  10474. if (charindex('7.00', @@version) = 0 and
  10475.     charindex('8.00', @@version) = 0)
  10476. begin
  10477.     print ''
  10478.     print ''
  10479.     print 'Warning:'
  10480.     print 'you are installing the stored procedures '
  10481.     print 'on a pre 7.0 SQL Server.'
  10482.     print 'Ignore the following errors.'
  10483. end
  10484. go
  10485.  
  10486. create procedure sp_ddopen; 12(
  10487.                @handle          int output,
  10488.                @procname        sysname,
  10489.                @scrollopt       int output,
  10490.                @ccopt           int output,
  10491.                @rows            int output,
  10492.                @p1              nvarchar(774),
  10493.                @p2              nvarchar(774),
  10494.                @p3              nvarchar(774),
  10495.                @p4              nvarchar(774),
  10496.                @p5              nvarchar(774))
  10497. as
  10498.     set nocount on
  10499.     declare @ret int
  10500.  
  10501.     create table #sptables (
  10502.         TABLE_QUALIFIER sysname null,
  10503.         TABLE_OWNER sysname null,
  10504.         TABLE_NAME sysname null,
  10505.         TABLE_TYPE  varchar(32) null,
  10506.         REMARKS varchar(254) null)
  10507.     if @procname = 'sp_tables'
  10508.     begin
  10509.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  10510.     end
  10511.     else
  10512.     begin
  10513.         insert into #sptables exec sp_tables_ex @p1,@p2,@p3,@p4,@p5
  10514.     end
  10515.     exec @ret = sp_cursoropen @handle output,
  10516.         'select * from #sptables',
  10517.         @scrollopt output, @ccopt output, @rows output
  10518.     drop table #sptables
  10519.     return @ret
  10520. go
  10521.  
  10522. if (charindex('7.00', @@version) = 0 and
  10523.     charindex('8.00', @@version) = 0)
  10524. begin
  10525.     print ''
  10526.     print ''
  10527.     print 'Warning:'
  10528.     print 'you are installing the stored procedures '
  10529.     print 'on a pre 7.0 SQL Server.'
  10530.     print 'Ignore the following errors.'
  10531. end
  10532. go
  10533.  
  10534. create procedure sp_ddopen; 13(
  10535.                @handle          int output,
  10536.                @procname        sysname,
  10537.                @scrollopt       int output,
  10538.                @ccopt           int output,
  10539.                @rows            int output,
  10540.                @p1              nvarchar(774),  -- @table_name
  10541.                @p2              nvarchar(774),  -- @table_owner
  10542.                @p3              nvarchar(774),  -- @table_qualifier
  10543.                @p4              nvarchar(774))  -- @table_type
  10544. as
  10545.     -- this code was copied from sp_tableswc (for 7,0).  Any changes
  10546.     -- made here need to be evaluated for porting to sp_tableswc.
  10547.     set nocount on
  10548.     declare @ret int
  10549.     declare @databasename   sysname
  10550.     declare @qualprocname   nvarchar(141) /* 128 + '..sp_tables' */
  10551.     
  10552.     create table #sptables (
  10553.         TABLE_QUALIFIER sysname null,
  10554.         TABLE_OWNER sysname null,
  10555.         TABLE_NAME sysname null,
  10556.         TABLE_TYPE varchar(32) null,
  10557.         REMARKS varchar(254) null)
  10558.         
  10559.     declare databases CURSOR FOR
  10560.         select name from master..sysdatabases
  10561.         where name like @p3 and name <> 'model' and has_dbaccess(name)=1
  10562.         for read only
  10563.  
  10564.     open databases
  10565.     fetch next from databases into @databasename
  10566.     while (@@FETCH_STATUS <> -1)
  10567.     begin
  10568.         if (charindex('%', @databasename) = 0)
  10569.         begin   /* skip dbnames w/wildcard characters to prevent loop */
  10570.             select @qualprocname = @databasename + '..sp_tables'
  10571.             insert into #sptables exec @qualprocname
  10572.                 @p1, @p2, @databasename, @p3
  10573.         end
  10574.         fetch next from databases into @databasename
  10575.     end
  10576.     deallocate databases
  10577.     
  10578.     exec @ret = sp_cursoropen @handle output,
  10579.         'select * from #sptables',
  10580.         @scrollopt output, @ccopt output, @rows output
  10581.     drop table #sptables
  10582.     return @ret
  10583. go
  10584.  
  10585. if (charindex('8.00', @@version) > 0)
  10586.     drop procedure sp_ddopen
  10587. else
  10588. begin
  10589.     print ''
  10590.     print ''
  10591.     print 'Warning:'
  10592.     print 'you are installing the stored procedures '
  10593.     print 'on a pre 8.0 SQL Server.'
  10594.     print 'Ignore the following errors.'
  10595. end
  10596. go
  10597.  
  10598. /*  Procedure for 8.0 server */
  10599. create procedure sp_ddopen; 1(
  10600.                @handle          int output,
  10601.                @procname        sysname,
  10602.                @scrollopt       int output,
  10603.                @ccopt           int output,
  10604.                @rows            int output,
  10605.                @p1              nvarchar(774) = null,
  10606.                @p2              nvarchar(774) = null,
  10607.                @p3              nvarchar(774) = null,
  10608.                @p4              nvarchar(774) = null,
  10609.                @p5              nvarchar(774) = null,
  10610.                @p6              nvarchar(774) = null,
  10611.                @p7              int = null,
  10612.                @ODBCVer         int = 2)
  10613. as
  10614.     set nocount on
  10615.     declare @ret int
  10616.  
  10617.     if @procname = 'sp_column_privileges'
  10618.     begin
  10619.         exec @ret = sp_ddopen;2 @handle output, 
  10620.                             @scrollopt output, 
  10621.                             @ccopt output, 
  10622.                             @rows output, 
  10623.                             @p1,
  10624.                             @p2,
  10625.                             @p3,
  10626.                             @p4
  10627.     end
  10628.     else if @procname = 'sp_columns' or @procname = 'sp_columns_ex'
  10629.     begin
  10630.         exec @ret = sp_ddopen;3 @handle output,
  10631.                             @procname,
  10632.                             @scrollopt output, 
  10633.                             @ccopt output, 
  10634.                             @rows output, 
  10635.                             @p1,
  10636.                             @p2,
  10637.                             @p3,
  10638.                             @p4,
  10639.                             @p5,
  10640.                             @ODBCVer
  10641.     end
  10642.     else if @procname = 'sp_datatype_info'
  10643.     begin
  10644.         exec @ret = sp_ddopen;4 @handle output,
  10645.                             @scrollopt output, 
  10646.                             @ccopt output, 
  10647.                             @rows output, 
  10648.                             @p7,
  10649.                             @ODBCVer
  10650.     end
  10651.     else if @procname = 'sp_fkeys'
  10652.     begin
  10653.         exec @ret = sp_ddopen;5 @handle output, 
  10654.                             @scrollopt output, 
  10655.                             @ccopt output, 
  10656.                             @rows output, 
  10657.                             @p1,
  10658.                             @p2,
  10659.                             @p3,
  10660.                             @p4,
  10661.                             @p5,
  10662.                             @p6
  10663.     end
  10664.     else if @procname = 'sp_pkeys'
  10665.     begin
  10666.         exec @ret = sp_ddopen;6 @handle output, 
  10667.                             @scrollopt output, 
  10668.                             @ccopt output, 
  10669.                             @rows output, 
  10670.                             @p1,
  10671.                             @p2,
  10672.                             @p3
  10673.     end
  10674.     else if @procname = 'sp_special_columns'
  10675.     begin
  10676.         exec @ret = sp_ddopen;7 @handle output, 
  10677.                             @scrollopt output, 
  10678.                             @ccopt output, 
  10679.                             @rows output, 
  10680.                             @p1,
  10681.                             @p2,
  10682.                             @p3,
  10683.                             @p4,
  10684.                             @p5,
  10685.                             @p6,
  10686.                             @ODBCVer
  10687.     end
  10688.     else if @procname = 'sp_sproc_columns'
  10689.     begin
  10690.         exec @ret = sp_ddopen;8 @handle output, 
  10691.                             @scrollopt output, 
  10692.                             @ccopt output, 
  10693.                             @rows output, 
  10694.                             @p1,
  10695.                             @p2,
  10696.                             @p3,
  10697.                             @p4,
  10698.                             @ODBCVer
  10699.     end
  10700.     else if @procname = 'sp_statistics'
  10701.     begin
  10702.         exec @ret = sp_ddopen;9 @handle output, 
  10703.                             @scrollopt output, 
  10704.                             @ccopt output, 
  10705.                             @rows output, 
  10706.                             @p1,
  10707.                             @p2,
  10708.                             @p3,
  10709.                             @p4,
  10710.                             @p5,
  10711.                             @p6
  10712.     end
  10713.     else if @procname = 'sp_stored_procedures'
  10714.     begin
  10715.         exec @ret = sp_ddopen;10 @handle output, 
  10716.                              @scrollopt output, 
  10717.                              @ccopt output, 
  10718.                              @rows output, 
  10719.                              @p1,
  10720.                              @p2,
  10721.                              @p3
  10722.     end
  10723.     else if @procname = 'sp_table_privileges'
  10724.     begin
  10725.         exec @ret = sp_ddopen;11 @handle output, 
  10726.                              @scrollopt output, 
  10727.                              @ccopt output, 
  10728.                              @rows output, 
  10729.                              @p1,
  10730.                              @p2,
  10731.                              @p3
  10732.     end
  10733.     else if @procname = 'sp_tables' or @procname = 'sp_tables_ex'
  10734.     begin
  10735.         exec @ret = sp_ddopen;12 @handle output,
  10736.                             @procname,
  10737.                             @scrollopt output, 
  10738.                             @ccopt output, 
  10739.                             @rows output, 
  10740.                             @p1,
  10741.                             @p2,
  10742.                             @p3,
  10743.                             @p4,
  10744.                             @p5
  10745.     end
  10746.     else if @procname = 'sp_tableswc'
  10747.     begin
  10748.         exec @ret = sp_ddopen;13 @handle output,
  10749.                             @procname,
  10750.                             @scrollopt output,
  10751.                             @ccopt output,
  10752.                             @rows output,
  10753.                             @p1,
  10754.                             @p2,
  10755.                             @p3,
  10756.                             @p4
  10757.     end
  10758.     else
  10759.         print 'Unknown sp_ddopen procedure'
  10760.     select @ret = isnull(@ret,0)
  10761.     return isnull(@ret,0)
  10762. go
  10763.  
  10764.  
  10765. if (charindex('8.00', @@version) = 0)
  10766. begin
  10767.     print ''
  10768.     print ''
  10769.     print 'Warning:'
  10770.     print 'you are installing the stored procedures '
  10771.     print 'on a pre 8.0 SQL Server.'
  10772.     print 'Ignore the following errors.'
  10773. end
  10774. go
  10775.  
  10776. create procedure sp_ddopen; 2(
  10777.                @handle          int output,
  10778.                @scrollopt       int output,
  10779.                @ccopt           int output,
  10780.                @rows            int output,
  10781.                @p1              nvarchar(774),
  10782.                @p2              nvarchar(774),
  10783.                @p3              nvarchar(774),
  10784.                @p4              nvarchar(774))
  10785. as
  10786.     set nocount on
  10787.     declare @ret int
  10788.  
  10789.     create table #spcolpriv (
  10790.         TABLE_QUALIFIER sysname collate database_default null,
  10791.         TABLE_OWNER sysname collate database_default null,
  10792.         TABLE_NAME sysname collate database_default not null,
  10793.         COLUMN_NAME sysname collate database_default not null,
  10794.         GRANTOR sysname collate database_default null,
  10795.         GRANTEE sysname collate database_default not null,
  10796.         PRIVILEGE varchar(32) collate database_default not null,
  10797.         IS_GRANTABLE varchar(3) collate database_default null
  10798.         )
  10799.     insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  10800.     exec @ret = sp_cursoropen @handle output,
  10801.         'select * from #spcolpriv',
  10802.         @scrollopt output, @ccopt output, @rows output
  10803.     drop table #spcolpriv
  10804.     return @ret
  10805. go
  10806.  
  10807.  
  10808. if (charindex('8.00', @@version) = 0)
  10809. begin
  10810.     print ''
  10811.     print ''
  10812.     print 'Warning:'
  10813.     print 'you are installing the stored procedures '
  10814.     print 'on a pre 8.0 SQL Server.'
  10815.     print 'Ignore the following errors.'
  10816. end
  10817. go
  10818.  
  10819. create procedure sp_ddopen; 3(
  10820.                @handle          int output,
  10821.                @procname        sysname,
  10822.                @scrollopt       int output,
  10823.                @ccopt           int output,
  10824.                @rows            int output,
  10825.                @p1              nvarchar(774),
  10826.                @p2              nvarchar(774),
  10827.                @p3              nvarchar(774),
  10828.                @p4              nvarchar(774),
  10829.                @p5              nvarchar(774),
  10830.                @ODBCVer         int)
  10831. as
  10832.     set nocount on
  10833.     declare @ret int
  10834.  
  10835.     create table #spcolumns (
  10836.         TABLE_QUALIFIER sysname collate database_default null,
  10837.         TABLE_OWNER sysname collate database_default null,
  10838.         TABLE_NAME sysname collate database_default not null,
  10839.         COLUMN_NAME sysname collate database_default not null,
  10840.         DATA_TYPE smallint not null,
  10841.         TYPE_NAME sysname collate database_default not null,
  10842.         "PRECISION" int null,
  10843.         LENGTH int null,
  10844.         SCALE smallint null,
  10845.         RADIX smallint null,
  10846.         NULLABLE smallint not null,
  10847.         REMARKS varchar(254) collate database_default null,
  10848.         COLUMN_DEF nvarchar(3000) collate database_default null,
  10849.         SQL_DATA_TYPE smallint null,
  10850.         SQL_DATETIME_SUB smallint null,
  10851.         CHAR_OCTET_LENGTH int null,
  10852.         ORDINAL_POSITION int not null,
  10853.         IS_NULLABLE varchar(254) collate database_default null,
  10854.         SS_DATA_TYPE tinyint null
  10855.         )
  10856.  
  10857.     if @procname = 'sp_columns'
  10858.     begin
  10859.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  10860.     end
  10861.     else
  10862.     begin
  10863.         insert into #spcolumns exec sp_columns_ex @p1,@p2,@p3,@p4,@p5,@ODBCVer
  10864.     end
  10865.     exec @ret = sp_cursoropen @handle output,
  10866.         'select * from #spcolumns',
  10867.         @scrollopt output, @ccopt output, @rows output
  10868.     drop table #spcolumns
  10869.     return @ret
  10870. go
  10871.  
  10872.  
  10873.  
  10874. if (charindex('8.00', @@version) = 0)
  10875. begin
  10876.     print ''
  10877.     print ''
  10878.     print 'Warning:'
  10879.     print 'you are installing the stored procedures '
  10880.     print 'on a pre 8.0 SQL Server.'
  10881.     print 'Ignore the following errors.'
  10882. end
  10883. go
  10884.  
  10885. create procedure sp_ddopen; 4(
  10886.                @handle          int output,
  10887.                @scrollopt       int output,
  10888.                @ccopt           int output,
  10889.                @rows            int output,
  10890.                @p7              int,
  10891.                @ODBCVer         int)
  10892. as
  10893.     set nocount on
  10894.     declare @ret int
  10895.  
  10896.     create table #spdatatypeinfo (
  10897.         TYPE_NAME           sysname  collate database_default not null,
  10898.         DATA_TYPE           smallint not null,
  10899.         "PRECISION"         int null,
  10900.         LITERAL_PREFIX      varchar(32) collate database_default null,
  10901.         LITERAL_SUFFIX      varchar(32)    collate database_default null,
  10902.         CREATE_PARAMS       varchar(32)    collate database_default null,
  10903.         NULLABLE            smallint   not null,
  10904.         CASE_SENSITIVE      smallint   not null,
  10905.         SEARCHABLE          smallint   not null,
  10906.         UNSIGNED_ATTRIBUTE  smallint   null,
  10907.         MONEY   smallint    not null,
  10908.         AUTO_INCREMENT      smallint    null,
  10909.         LOCAL_TYPE_NAME     sysname collate database_default null,
  10910.         MINIMUM_SCALE       smallint     null,
  10911.         MAXIMUM_SCALE       smallint   null,
  10912.         SQL_DATA_TYPE       smallint      not null,
  10913.         SQL_DATETIME_SUB    smallint   null,
  10914.         NUM_PREC_RADIX      int  null,
  10915.         INTERVAL_PRECISION  smallint    NULL,
  10916.         USERTYPE            smallint not null)
  10917.  
  10918.     insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  10919.     exec @ret = sp_cursoropen @handle output,
  10920.         'select * from #spdatatypeinfo',
  10921.         @scrollopt output, @ccopt output, @rows output
  10922.     drop table #spdatatypeinfo
  10923.     return @ret
  10924. go
  10925.  
  10926.  
  10927.  
  10928. if (charindex('8.00', @@version) = 0)
  10929. begin
  10930.     print ''
  10931.     print ''
  10932.     print 'Warning:'
  10933.     print 'you are installing the stored procedures '
  10934.     print 'on a pre 8.0 SQL Server.'
  10935.     print 'Ignore the following errors.'
  10936. end
  10937. go
  10938.  
  10939. create procedure sp_ddopen; 5(
  10940.                @handle          int output,
  10941.                @scrollopt       int output,
  10942.                @ccopt           int output,
  10943.                @rows            int output,
  10944.                @p1              nvarchar(774),
  10945.                @p2              nvarchar(774),
  10946.                @p3              nvarchar(774),
  10947.                @p4              nvarchar(774),
  10948.                @p5              nvarchar(774),
  10949.                @p6              nvarchar(774))
  10950. as
  10951.     set nocount on
  10952.     declare @ret int
  10953.  
  10954.     create table #spfkeys (
  10955.         PKTABLE_QUALIFIER sysname    collate database_default null,
  10956.         PKTABLE_OWNER sysname   collate database_default null,
  10957.         PKTABLE_NAME sysname  collate database_default not null,
  10958.         PKCOLUMN_NAME sysname  collate database_default not null,
  10959.         FKTABLE_QUALIFIER sysname   collate database_default null,
  10960.         FKTABLE_OWNER sysname   collate database_default null,
  10961.         FKTABLE_NAME sysname  collate database_default not null,
  10962.         FKCOLUMN_NAME sysname  collate database_default not null,
  10963.         KEY_SEQ smallint not null,
  10964.         UPDATE_RULE smallint null,
  10965.         DELETE_RULE smallint null,
  10966.         FK_NAME sysname collate database_default null,
  10967.         PK_NAME sysname collate database_default null,
  10968.         DEFERRABILITY smallint null
  10969.         )
  10970.     insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  10971.     exec @ret = sp_cursoropen @handle output,
  10972.         'select * from #spfkeys',
  10973.         @scrollopt output, @ccopt output, @rows output
  10974.     drop table #spfkeys
  10975.     return @ret
  10976. go
  10977.  
  10978.  
  10979.  
  10980. if (charindex('8.00', @@version) = 0)
  10981. begin
  10982.     print ''
  10983.     print ''
  10984.     print 'Warning:'
  10985.     print 'you are installing the stored procedures '
  10986.     print 'on a pre 8.0 SQL Server.'
  10987.     print 'Ignore the following errors.'
  10988. end
  10989. go
  10990.  
  10991. create procedure sp_ddopen; 6(
  10992.                @handle          int output,
  10993.                @scrollopt       int output,
  10994.                @ccopt           int output,
  10995.                @rows            int output,
  10996.                @p1              nvarchar(774),
  10997.                @p2              nvarchar(774),
  10998.                @p3              nvarchar(774))
  10999. as
  11000.     set nocount on
  11001.     declare @ret int
  11002.  
  11003.     create table #sppkeys (
  11004.         TABLE_QUALIFIER sysname   collate database_default null,
  11005.         TABLE_OWNER sysname   collate database_default null,
  11006.         TABLE_NAME sysname  collate database_default not null,
  11007.         COLUMN_NAME sysname  collate database_default not null,
  11008.         KEY_SEQ smallint not null,
  11009.         PK_NAME sysname collate database_default null
  11010.         )
  11011.     insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  11012.     exec @ret = sp_cursoropen @handle output,
  11013.         'select * from #sppkeys',
  11014.         @scrollopt output, @ccopt output, @rows output
  11015.     drop table #sppkeys
  11016.     return @ret
  11017. go
  11018.  
  11019.  
  11020.  
  11021. if (charindex('8.00', @@version) = 0)
  11022. begin
  11023.     print ''
  11024.     print ''
  11025.     print 'Warning:'
  11026.     print 'you are installing the stored procedures '
  11027.     print 'on a pre 8.0 SQL Server.'
  11028.     print 'Ignore the following errors.'
  11029. end
  11030. go
  11031.  
  11032. create procedure sp_ddopen; 7(
  11033.                @handle          int output,
  11034.                @scrollopt       int output,
  11035.                @ccopt           int output,
  11036.                @rows            int output,
  11037.                @p1              nvarchar(774),
  11038.                @p2              nvarchar(774),
  11039.                @p3              nvarchar(774),
  11040.                @p4              nvarchar(774),
  11041.                @p5              nvarchar(774),
  11042.                @p6              nvarchar(774),
  11043.                @ODBCVer         int)
  11044. as
  11045.     set nocount on
  11046.     declare @ret int
  11047.  
  11048.     create table #spspeccol (
  11049.         SCOPE smallint null,
  11050.         COLUMN_NAME sysname collate database_default not null,
  11051.         DATA_TYPE smallint not null,
  11052.         TYPE_NAME sysname collate database_default not null,
  11053.         "PRECISION" int null,
  11054.         LENGTH int null,
  11055.         SCALE smallint null,
  11056.         PSEUDO_COLUMN smallint null
  11057.         )
  11058.     insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  11059.     exec @ret = sp_cursoropen @handle output,
  11060.         'select * from #spspeccol',
  11061.         @scrollopt output, @ccopt output, @rows output
  11062.     drop table #spspeccol
  11063.     return @ret
  11064. go
  11065.  
  11066.  
  11067.  
  11068. if (charindex('8.00', @@version) = 0)
  11069. begin
  11070.     print ''
  11071.     print ''
  11072.     print 'Warning:'
  11073.     print 'you are installing the stored procedures '
  11074.     print 'on a pre 8.0 SQL Server.'
  11075.     print 'Ignore the following errors.'
  11076. end
  11077. go
  11078.  
  11079. create procedure sp_ddopen; 8(
  11080.                @handle          int output,
  11081.                @scrollopt       int output,
  11082.                @ccopt           int output,
  11083.                @rows            int output,
  11084.                @p1              nvarchar(774),
  11085.                @p2              nvarchar(774),
  11086.                @p3              nvarchar(774),
  11087.                @p4              nvarchar(774),
  11088.                @ODBCVer         int)
  11089. as
  11090.     set nocount on
  11091.     declare @ret int
  11092.  
  11093.     create table #spproccol (
  11094.         PROCEDURE_QUALIFIER sysname  collate database_default null,
  11095.         PROCEDURE_OWNER sysname  collate database_default null,
  11096.         PROCEDURE_NAME sysname collate database_default not null,
  11097.         COLUMN_NAME sysname collate database_default not null,
  11098.         COLUMN_TYPE smallint not null,
  11099.         DATA_TYPE smallint null,
  11100.         TYPE_NAME sysname collate database_default not null,
  11101.         "PRECISION" int null,
  11102.         LENGTH int null,
  11103.         SCALE smallint null,
  11104.         RADIX smallint null,
  11105.         NULLABLE smallint not null,
  11106.         REMARKS varchar(254) collate database_default null,
  11107.         COLUMN_DEF nvarchar(3000) collate database_default null,
  11108.         SQL_DATA_TYPE smallint null,
  11109.         SQL_DATETIME_SUB smallint null,
  11110.         CHAR_OCTET_LENGTH int null,
  11111.         ORDINAL_POSITION int not null,
  11112.         IS_NULLABLE varchar(254) collate database_default null,
  11113.         SS_DATA_TYPE tinyint null
  11114.         )
  11115.     insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  11116.     exec @ret = sp_cursoropen @handle output,
  11117.         'select * from #spproccol',
  11118.         @scrollopt output, @ccopt output, @rows output
  11119.     drop table #spproccol
  11120.     return @ret
  11121. go
  11122.  
  11123.  
  11124.  
  11125. if (charindex('8.00', @@version) = 0)
  11126. begin
  11127.     print ''
  11128.     print ''
  11129.     print 'Warning:'
  11130.     print 'you are installing the stored procedures '
  11131.     print 'on a pre 8.0 SQL Server.'
  11132.     print 'Ignore the following errors.'
  11133. end
  11134. go
  11135.  
  11136. create procedure sp_ddopen; 9(
  11137.                @handle          int output,
  11138.                @scrollopt       int output,
  11139.                @ccopt           int output,
  11140.                @rows            int output,
  11141.                @p1              nvarchar(774),
  11142.                @p2              nvarchar(774),
  11143.                @p3              nvarchar(774),
  11144.                @p4              nvarchar(774),
  11145.                @p5              nvarchar(774),
  11146.                @p6              nvarchar(774))
  11147. as
  11148.     set nocount on
  11149.     declare @ret int
  11150.  
  11151.     create table #spstatistics (
  11152.         TABLE_QUALIFIER sysname   collate database_default null,
  11153.         TABLE_OWNER sysname   collate database_default null,
  11154.         TABLE_NAME sysname  collate database_default not null,
  11155.         NON_UNIQUE smallint null,
  11156.         INDEX_QUALIFIER sysname collate database_default null,
  11157.         INDEX_NAME sysname collate database_default null,
  11158.         TYPE smallint not null,
  11159.         SEQ_IN_INDEX smallint null,
  11160.         COLUMN_NAME sysname collate database_default null,
  11161.         COLLATION char(1) collate database_default null,
  11162.         CARDINALITY int null,
  11163.         PAGES int null,
  11164.         FILTER_CONDITION varchar(128) collate database_default null
  11165.         )
  11166.     insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  11167.     exec @ret = sp_cursoropen @handle output,
  11168.         'select * from #spstatistics',
  11169.         @scrollopt output, @ccopt output, @rows output
  11170.     drop table #spstatistics
  11171.     return @ret
  11172. go
  11173.                
  11174.  
  11175.  
  11176. if (charindex('8.00', @@version) = 0)
  11177. begin
  11178.     print ''
  11179.     print ''
  11180.     print 'Warning:'
  11181.     print 'you are installing the stored procedures '
  11182.     print 'on a pre 8.0 SQL Server.'
  11183.     print 'Ignore the following errors.'
  11184. end
  11185. go
  11186.  
  11187. create procedure sp_ddopen; 10(
  11188.                @handle          int output,
  11189.                @scrollopt       int output,
  11190.                @ccopt           int output,
  11191.                @rows            int output,
  11192.                @p1              nvarchar(774),
  11193.                @p2              nvarchar(774),
  11194.                @p3              nvarchar(774))
  11195. as
  11196.     set nocount on
  11197.     declare @ret int
  11198.  
  11199.     create table #spprocedures (
  11200.         PROCEDURE_QUALIFIER sysname  collate database_default null,
  11201.         PROCEDURE_OWNER sysname  collate database_default null,
  11202.         PROCEDURE_NAME nvarchar(134) collate database_default not null, /*134=sysname+';'+ltrim(str(c.number,5))*/
  11203.         NUM_INPUT_PARAMS int null,
  11204.         NUM_OUTPUT_PARAMS int null,
  11205.         NUM_RESULT_SETS int null,
  11206.         REMARKS varchar(254) collate database_default null,
  11207.         PROCEDURE_TYPE smallint null
  11208.         )
  11209.     insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  11210.     exec @ret = sp_cursoropen @handle output,
  11211.         'select * from #spprocedures',
  11212.         @scrollopt output, @ccopt output, @rows output
  11213.     drop table #spprocedures
  11214.     return @ret
  11215. go
  11216.  
  11217.  
  11218.  
  11219. if (charindex('8.00', @@version) = 0)
  11220. begin
  11221.     print ''
  11222.     print ''
  11223.     print 'Warning:'
  11224.     print 'you are installing the stored procedures '
  11225.     print 'on a pre 8.0 SQL Server.'
  11226.     print 'Ignore the following errors.'
  11227. end
  11228. go
  11229.  
  11230. create procedure sp_ddopen; 11(
  11231.                @handle          int output,
  11232.                @scrollopt       int output,
  11233.                @ccopt           int output,
  11234.                @rows            int output,
  11235.                @p1              nvarchar(774),
  11236.                @p2              nvarchar(774),
  11237.                @p3              nvarchar(774))
  11238. as
  11239.     set nocount on
  11240.     declare @ret int
  11241.  
  11242.     create table #sptabpriv (
  11243.         TABLE_QUALIFIER sysname collate database_default null,
  11244.         TABLE_OWNER sysname collate database_default null,
  11245.         TABLE_NAME sysname collate database_default not null,
  11246.         GRANTOR sysname collate database_default null,
  11247.         GRANTEE sysname collate database_default not null,
  11248.         PRIVILEGE varchar(32) collate database_default not null,
  11249.         IS_GRANTABLE varchar(3) collate database_default null
  11250.         )
  11251.     insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  11252.     exec @ret = sp_cursoropen @handle output,
  11253.         'select * from #sptabpriv',
  11254.         @scrollopt output, @ccopt output, @rows output
  11255.     drop table #sptabpriv
  11256.     return @ret
  11257. go
  11258.  
  11259.  
  11260.  
  11261. if (charindex('8.00', @@version) = 0)
  11262. begin
  11263.     print ''
  11264.     print ''
  11265.     print 'Warning:'
  11266.     print 'you are installing the stored procedures '
  11267.     print 'on a pre 8.0 SQL Server.'
  11268.     print 'Ignore the following errors.'
  11269. end
  11270. go
  11271.  
  11272. create procedure sp_ddopen; 12(
  11273.                @handle          int output,
  11274.                @procname        sysname,
  11275.                @scrollopt       int output,
  11276.                @ccopt           int output,
  11277.                @rows            int output,
  11278.                @p1              nvarchar(774),
  11279.                @p2              nvarchar(774),
  11280.                @p3              nvarchar(774),
  11281.                @p4              nvarchar(774),
  11282.                @p5              nvarchar(774))
  11283. as
  11284.     set nocount on
  11285.     declare @ret int
  11286.  
  11287.     create table #sptables (
  11288.         TABLE_QUALIFIER sysname collate database_default null,
  11289.         TABLE_OWNER sysname collate database_default null,
  11290.         TABLE_NAME sysname collate database_default null,
  11291.         TABLE_TYPE  varchar(32) collate database_default null,
  11292.         REMARKS varchar(254) collate database_default null)
  11293.     if @procname = 'sp_tables'
  11294.     begin
  11295.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  11296.     end
  11297.     else
  11298.     begin
  11299.         insert into #sptables exec sp_tables_ex @p1,@p2,@p3,@p4,@p5
  11300.     end
  11301.     exec @ret = sp_cursoropen @handle output,
  11302.         'select * from #sptables',
  11303.         @scrollopt output, @ccopt output, @rows output
  11304.     drop table #sptables
  11305.     return @ret
  11306. go
  11307.  
  11308. create procedure sp_ddopen; 13(
  11309.                @handle          int output,
  11310.                @procname        sysname,
  11311.                @scrollopt       int output,
  11312.                @ccopt           int output,
  11313.                @rows            int output,
  11314.                @p1              nvarchar(774),  -- @table_name
  11315.                @p2              nvarchar(774),  -- @table_owner
  11316.                @p3              nvarchar(774),  -- @table_qualifier
  11317.                @p4              nvarchar(774))  -- @table_type
  11318. as
  11319.     -- this code was copied from sp_tableswc (for 8.0).  Any changes
  11320.     -- made here need to be evaluated for porting to sp_tableswc.
  11321.     set nocount on
  11322.     declare @ret int
  11323.     declare @databasename   sysname
  11324.     declare @qualprocname   nvarchar(141) /* 128 + '..sp_tables' */
  11325.  
  11326.     create table #sptables (
  11327.         TABLE_QUALIFIER sysname collate database_default null,
  11328.         TABLE_OWNER sysname collate database_default null,
  11329.         TABLE_NAME sysname collate database_default null,
  11330.         TABLE_TYPE  varchar(32) collate database_default null,
  11331.         REMARKS varchar(254) collate database_default null)
  11332.  
  11333.     declare databases CURSOR FOR
  11334.         select name from master..sysdatabases
  11335.         where name like @p3 and name <> 'model' and has_dbaccess(name)=1
  11336.         for read only
  11337.  
  11338.     open databases
  11339.     fetch next from databases into @databasename
  11340.     while (@@FETCH_STATUS <> -1)
  11341.     begin
  11342.         if (charindex('%', @databasename) = 0)
  11343.         begin   /* skip dbnames w/wildcard characters to prevent loop */
  11344.             select @qualprocname = @databasename + '..sp_tables'
  11345.             insert into #sptables exec @qualprocname
  11346.                 @p1, @p2, @databasename, @p4
  11347.         end
  11348.         fetch next from databases into @databasename
  11349.     end
  11350.     deallocate databases
  11351.     exec @ret = sp_cursoropen @handle output,
  11352.         'select * from #sptables',
  11353.         @scrollopt output, @ccopt output, @rows output
  11354.     drop table #sptables
  11355.     return @ret
  11356. go
  11357.  
  11358. grant execute on sp_ddopen to public
  11359. go
  11360.  
  11361. print 'creating sp_tableswc'
  11362. go
  11363.  
  11364. if (charindex('7.00', @@version) = 0 and
  11365.     charindex('8.00', @@version) = 0)
  11366. begin
  11367.     print ''
  11368.     print ''
  11369.     print 'Warning:'
  11370.     print 'you are installing the stored procedures '
  11371.     print 'on a pre 7.0 SQL Server.'
  11372.     print 'Ignore the following errors.'
  11373. end
  11374. go
  11375.  
  11376. /*  Procedure for 7.0 server */
  11377. create procedure sp_tableswc(
  11378.                @table_name      nvarchar(384)   = null,
  11379.                @table_owner     nvarchar(384)   = null,
  11380.                @table_qualifier sysname = null,
  11381.                @table_type      varchar(100) = null)
  11382. as
  11383.     -- sp_ddopen; 13 contains the same code, so changes made here should
  11384.     -- be evaluated for porting to sp_ddopen; 13
  11385.     declare @databasename   sysname
  11386.     declare @qualprocname   nvarchar(141) /* 128 + '..sp_tables' */
  11387.  
  11388.     create table #sptables (
  11389.         TABLE_QUALIFIER sysname null,
  11390.         TABLE_OWNER sysname null,
  11391.         TABLE_NAME sysname null,
  11392.         TABLE_TYPE  varchar(32) null,
  11393.         REMARKS varchar(254) null)
  11394.  
  11395.     declare databases CURSOR FOR
  11396.         select name from master..sysdatabases
  11397.         where name like @table_qualifier and name <> 'model' and has_dbaccess(name)=1
  11398.         for read only
  11399.  
  11400.     open databases
  11401.     fetch next from databases into @databasename
  11402.     while (@@FETCH_STATUS <> -1)
  11403.     begin
  11404.         if (charindex('%', @databasename) = 0)
  11405.         begin   /* skip dbnames w/wildcard characters to prevent loop */
  11406.             select @qualprocname = @databasename + '..sp_tables'
  11407.             insert into #sptables exec @qualprocname
  11408.                 @table_name, @table_owner, @databasename, @table_type
  11409.         end
  11410.         fetch next from databases into @databasename
  11411.     end
  11412.     deallocate databases
  11413.     select * from #sptables
  11414.         order by 4, 1, 2, 3
  11415. go
  11416.  
  11417. if (charindex('8.00', @@version) = 0)
  11418. begin
  11419.     print ''
  11420.     print ''
  11421.     print 'Warning:'
  11422.     print 'you are installing the stored procedures '
  11423.     print 'on a pre 8.0 SQL Server.'
  11424.     print 'Ignore the following errors.'
  11425. end
  11426. else
  11427. drop procedure sp_tableswc
  11428. go
  11429.  
  11430. /*  Procedure for 8.0 server */
  11431. create procedure sp_tableswc(
  11432.                @table_name      nvarchar(384)   = null,
  11433.                @table_owner     nvarchar(384)   = null,
  11434.                @table_qualifier sysname = null,
  11435.                @table_type      varchar(100) = null)
  11436. as
  11437.     -- sp_ddopen; 13 contains the same code, so changes made here should
  11438.     -- be evaluated for porting to sp_ddopen; 13
  11439.     declare @databasename   sysname
  11440.     declare @qualprocname   nvarchar(141) /* 128 + '..sp_tables' */
  11441.  
  11442.     create table #sptables (
  11443.         TABLE_QUALIFIER sysname collate database_default null,
  11444.         TABLE_OWNER sysname collate database_default null,
  11445.         TABLE_NAME sysname collate database_default null,
  11446.         TABLE_TYPE  varchar(32) collate database_default null,
  11447.         REMARKS varchar(254) collate database_default null)
  11448.  
  11449.     declare databases CURSOR FOR
  11450.         select name from master..sysdatabases
  11451.         where name like @table_qualifier and name <> 'model' and has_dbaccess(name)=1
  11452.         for read only
  11453.  
  11454.     open databases
  11455.     fetch next from databases into @databasename
  11456.     while (@@FETCH_STATUS <> -1)
  11457.     begin
  11458.         if (charindex('%', @databasename) = 0)
  11459.         begin   /* skip dbnames w/wildcard characters to prevent loop */
  11460.             select @qualprocname = @databasename + '..sp_tables'
  11461.             insert into #sptables exec @qualprocname
  11462.                 @table_name, @table_owner, @databasename, @table_type
  11463.         end
  11464.         fetch next from databases into @databasename
  11465.     end
  11466.     deallocate databases
  11467.     select * from #sptables
  11468.         order by 4, 1, 2, 3
  11469. go
  11470.  
  11471. grant execute on sp_tableswc to public
  11472. go
  11473.  
  11474. dump tran master with no_log
  11475. go
  11476.  
  11477. /*-------------------------------------------------------------------------*/
  11478. /*-------------- CATALOG STORED PROCEDURES FOR SQLOLEDB  ------------------*/
  11479. /*-------------------------------------------------------------------------*/
  11480.  
  11481. print ''
  11482. print 'creating spt_provider_types'
  11483. go
  11484. if (charindex('7.00', @@version) = 0 and
  11485.     charindex('8.00', @@version) = 0)
  11486. begin /* Pre 8.0 Server */
  11487. create table spt_provider_types
  11488.     (
  11489.     ss_dtype        tinyint     not null,
  11490.     fixlen          int         null,       /* datatype len for variable, else null */
  11491.     type_name       sysname     not null,
  11492.     oledb_data_type     smallint    not null,
  11493.     best_match      bit     not null,
  11494.     is_nullable     tinyint     null,
  11495.     case_sensitive      bit     not null,
  11496.     fixed_prec_scale    bit     not null,
  11497.     is_long         bit     not null,
  11498.     auto_unique_value   tinyint     not null,
  11499.     data_precision      int         null,
  11500.     numeric_scale       smallint    null,       /* min scale if 6.0 */
  11501.     column_size     int         null,
  11502.     literal_prefix      varchar(32) null,
  11503.     literal_suffix      varchar(32)     null,
  11504.     searchable      int     not null,
  11505.     unsigned_attribute  tinyint     null,
  11506.     local_type_name     sysname     null
  11507.     )
  11508. print ''
  11509. print ''
  11510. print 'Warning:'
  11511. print 'you are installing the stored procedures '
  11512. print 'on a pre 8.0 SQL Server.'
  11513. print 'Ignore the following errors.'
  11514. end
  11515. go
  11516. if (charindex('7.00', @@version) > 0 or
  11517.     charindex('8.00', @@version) > 0)
  11518. begin /* 8.0 Server */
  11519. create table spt_provider_types
  11520.     (
  11521.     ss_dtype        tinyint     not null,
  11522.     fixlen          int         null,       /* datatype len for variable, else null */
  11523.     type_name       sysname     not null,
  11524.     oledb_data_type     smallint    not null,
  11525.     best_match      bit     not null,
  11526.     is_nullable     bit     null,
  11527.     case_sensitive      bit     not null,
  11528.     fixed_prec_scale    bit     not null,
  11529.     is_long         bit     not null,
  11530.     auto_unique_value   tinyint     not null,
  11531.     data_precision      int         null,
  11532.     numeric_scale       smallint    null,       /* min scale if 6.0 */
  11533.     column_size     int         null,
  11534.     literal_prefix      nvarchar(32)    null,
  11535.     literal_suffix      nvarchar(32)    null,
  11536.     searchable      int     not null,
  11537.     unsigned_attribute  tinyint     null,
  11538.     local_type_name     sysname     null
  11539.     )
  11540. end
  11541. go
  11542.  
  11543. grant select on spt_provider_types to public
  11544. go
  11545. dump tran master with no_log
  11546. go
  11547.  
  11548. /*
  11549. ** Insert the spt_provider_types rows for DBTYTPE_STR data types
  11550. */
  11551. begin tran
  11552.  
  11553. /* Get case sensitivity */
  11554. declare @case_sensitive bit
  11555. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  11556. /* Local Char */
  11557. insert into spt_provider_types values
  11558.     (
  11559.     47 /*SQLCHARACTER*/,        /* ss_dtype */
  11560.     0,              /* fixlen */
  11561.     'char',             /* type_name */
  11562.     129 /*DBTYPE_STR*/,     /* oledb_data_type */
  11563.     0,              /* best_match */
  11564.     null,               /* is_nullable */
  11565.     @case_sensitive,        /* case_sensitive */
  11566.     0,              /* fixed_prec_scale */
  11567.     0,              /* is_long */
  11568.     0,              /* auto_unique_value */
  11569.     null,               /* data_precision */
  11570.     null,               /* numeric_scale */
  11571.     null,               /* column_size */
  11572.     '''',               /* literal_prefix */
  11573.     '''',               /* literal_suffix */
  11574.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11575.     null,               /* unsigned_attribute */
  11576.     'char'              /* local_type_name */
  11577.     )
  11578.  
  11579. /* Local Varchar */
  11580. insert into spt_provider_types values
  11581.     (
  11582.     39 /*SQLVARCHAR*/,      /* ss_dtype */
  11583.     null,               /* fixlen */
  11584.     'varchar',          /* type_name */
  11585.     129 /*DBTYPE_STR*/,     /* oledb_data_type */
  11586.     1,              /* best_match */
  11587.     null,               /* is_nullable */
  11588.     @case_sensitive,        /* case_sensitive */
  11589.     0,              /* fixed_prec_scale */
  11590.     0,              /* is_long */
  11591.     0,              /* auto_unique_value */
  11592.     null,               /* data_precision */
  11593.     null,               /* numeric_scale */
  11594.     null,               /* column_size */
  11595.     '''',               /* literal_prefix */
  11596.     '''',               /* literal_suffix */
  11597.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11598.     null,               /* unsigned_attribute */
  11599.     'varchar'           /* local_type_name */
  11600.     )
  11601.  
  11602. /* Local Text */
  11603. insert into spt_provider_types values
  11604.     (
  11605.     35  /*SQLTEXT*/,        /* ss_dtype */
  11606.     null,               /* fixlen */
  11607.     'text',             /* type_name */
  11608.     129 /*DBTYPE_STR*/,     /* oledb_data_type */
  11609.     0,              /* best_match */
  11610.     null,               /* is_nullable */
  11611.     @case_sensitive,        /* case_sensitive */
  11612.     0,              /* fixed_prec_scale */
  11613.     1,              /* is_long */
  11614.     0,              /* auto_unique_value */
  11615.     null,               /* data_precision */
  11616.     null,               /* numeric_scale */
  11617.     2147483647,         /* column_size */
  11618.     '''',               /* literal_prefix */
  11619.     '''',               /* literal_suffix */
  11620.     2 /*DB_LIKE_ONLY*/,     /* searchable */
  11621.     null,               /* unsigned_attribute */
  11622.     'text'              /* local_type_name */
  11623.     )
  11624. commit tran
  11625. go
  11626. dump tran master with no_log
  11627. go
  11628.  
  11629. /*
  11630. ** Insert the spt_provider_types rows for DBTYTPE_BYTES data types
  11631. */
  11632. begin tran
  11633. /* Local Binary */
  11634. insert into spt_provider_types values
  11635.     (
  11636.     45 /*SQLBINARY*/,       /* ss_dtype */
  11637.     0,              /* fixlen */
  11638.     'binary',           /* type_name */
  11639.     128 /*DBTYPE_BYTES*/,       /* oledb_data_type */
  11640.     0,              /* best_match */
  11641.     null,               /* is_nullable */
  11642.     0,              /* case_sensitive */
  11643.     0,              /* fixed_prec_scale */
  11644.     0,              /* is_long */
  11645.     0,              /* auto_unique_value */
  11646.     null,               /* data_precision */
  11647.     null,               /* numeric_scale */
  11648.     null,               /* column_size */
  11649.     '0x',               /* literal_prefix */
  11650.     null,               /* literal_suffix */
  11651.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  11652.     null,               /* unsigned_attribute */
  11653.     'binary'            /* local_type_name */
  11654.     )
  11655.  
  11656. /* Local Varbinary */
  11657. insert into spt_provider_types values
  11658.     (
  11659.     37 /*SQLVARBINARY*/,        /* ss_dtype */
  11660.     null,               /* fixlen */
  11661.     'varbinary',            /* type_name */
  11662.     128 /*DBTYPE_BYTES*/,       /* oledb_data_type */
  11663.     1,              /* best_match */
  11664.     null,               /* is_nullable */
  11665.     0,              /* case_sensitive */
  11666.     0,              /* fixed_prec_scale */
  11667.     0,              /* is_long */
  11668.     0,              /* auto_unique_value */
  11669.     null,               /* data_precision */
  11670.     null,               /* numeric_scale */
  11671.     null,               /* column_size */
  11672.     '0x',               /* literal_prefix */
  11673.     null,               /* literal_suffix */
  11674.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  11675.     null,               /* unsigned_attribute */
  11676.     'varbinary'         /* local_type_name */
  11677.     )
  11678.  
  11679. /* Local Image */
  11680. insert into spt_provider_types values
  11681.     (
  11682.     34 /*SQLIMAGE*/,        /* ss_dtype */
  11683.     null,               /* fixlen */
  11684.     'image',            /* type_name */
  11685.     128 /*DBTYPE_BYTES*/,       /* oledb_data_type */
  11686.     0,              /* best_match */
  11687.     null,               /* is_nullable */
  11688.     0,              /* case_sensitive */
  11689.     0,              /* fixed_prec_scale */
  11690.     1,              /* is_long */
  11691.     0,              /* auto_unique_value */
  11692.     null,               /* data_precision */
  11693.     null,               /* numeric_scale */
  11694.     2147483647,         /* column_size */
  11695.     '0x',               /* literal_prefix */
  11696.     null,               /* literal_suffix */
  11697.     1 /*DB_UNSEARCHABLE*/,      /* searchable */
  11698.     null,               /* unsigned_attribute */
  11699.     'image'             /* local_type_name */
  11700.     )
  11701.  
  11702. commit tran
  11703. go
  11704. dump tran master with no_log
  11705. go
  11706.  
  11707. /*
  11708. ** Insert the spt_provider_types rows for DBTYPE_DBTIMESTAMP data types
  11709. */
  11710. begin tran
  11711.  
  11712. /* Local Datetime */
  11713. insert into spt_provider_types values
  11714.     (
  11715.     61 /*SQLDATETIME*/,     /* ss_dtype */
  11716.     8,              /* fixlen */
  11717.     'datetime',         /* type_name */
  11718.     135 /*DBTYPE_DBTIMESTAMP*/, /* oledb_data_type */
  11719.     1,              /* best_match */
  11720.     null,               /* is_nullable */
  11721.     0,              /* case_sensitive */
  11722.     0,              /* fixed_prec_scale */
  11723.     0,              /* is_long */
  11724.     0,              /* auto_unique_value */
  11725.     23,             /* data_precision */
  11726.     null,               /* numeric_scale */
  11727.     null,               /* column_size */
  11728.     '''',               /* literal_prefix */
  11729.     '''',               /* literal_suffix */
  11730.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11731.     null,               /* unsigned_attribute */
  11732.     'datetime'          /* local_type_name */
  11733.     )
  11734.  
  11735. /* Local Smalldatetime */
  11736. insert into spt_provider_types values
  11737.     (
  11738.     58 /*SQLDATETIM4*/,     /* ss_dtype */
  11739.     4,              /* fixlen */
  11740.     'smalldatetime',        /* type_name */
  11741.     135 /*DBTYPE_DBTIMESTAMP*/, /* oledb_data_type */
  11742.     0,              /* best_match */
  11743.     null,               /* is_nullable */
  11744.     0,              /* case_sensitive */
  11745.     0,              /* fixed_prec_scale */
  11746.     0,              /* is_long */
  11747.     0,              /* auto_unique_value */
  11748.     16,             /* data_precision */
  11749.     null,               /* numeric_scale */
  11750.     null,               /* column_size */
  11751.     '''',               /* literal_prefix */
  11752.     '''',               /* literal_suffix */
  11753.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11754.     null,               /* unsigned_attribute */
  11755.     'smalldatetime'         /* local_type_name */
  11756.     )
  11757.  
  11758. if (charindex('7.00', @@version) = 0 and
  11759.     charindex('8.00', @@version) = 0)
  11760.     begin   /*  Add nullable type for non-Sphinx server */
  11761.     
  11762.     /* Local Datetimn */
  11763.     insert into spt_provider_types values
  11764.         (
  11765.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  11766.         4,              /* fixlen */
  11767.         'smalldatetime',        /* type_name */
  11768.         135 /*DBTYPE_DBTIMESTAMP*/, /* oledb_data_type */
  11769.         0,              /* best_match */
  11770.         null,               /* is_nullable */
  11771.         0,              /* case_sensitive */
  11772.         0,              /* fixed_prec_scale */
  11773.         0,              /* is_long */
  11774.         0,              /* auto_unique_value */
  11775.         16,             /* data_precision */
  11776.         null,               /* numeric_scale */
  11777.         null,               /* column_size */
  11778.         '''',               /* literal_prefix */
  11779.         '''',               /* literal_suffix */
  11780.         4 /*DB_SEARCHABLE*/,        /* searchable */
  11781.         null,               /* unsigned_attribute */
  11782.         'smalldatetime'         /* local_type_name */
  11783.         )   
  11784.     insert into spt_provider_types values
  11785.         (
  11786.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  11787.         8,              /* fixlen */
  11788.         'datetime',         /* type_name */
  11789.         135 /*DBTYPE_DBTIMESTAMP*/, /* oledb_data_type */
  11790.         0,              /* best_match */
  11791.         null,               /* is_nullable */
  11792.         0,              /* case_sensitive */
  11793.         0,              /* fixed_prec_scale */
  11794.         0,              /* is_long */
  11795.         0,              /* auto_unique_value */
  11796.         23,             /* data_precision */
  11797.         null,               /* numeric_scale */
  11798.         null,               /* column_size */
  11799.         '''',               /* literal_prefix */
  11800.         '''',               /* literal_suffix */
  11801.         4 /*DB_SEARCHABLE*/,        /* searchable */
  11802.         null,               /* unsigned_attribute */
  11803.         'datetime'          /* local_type_name */
  11804.         )
  11805.     end
  11806.  
  11807. commit tran
  11808. go
  11809. dump tran master with no_log
  11810. go
  11811.  
  11812. /*
  11813. ** Insert the spt_provider_types rows for DBTYPE_CY data types
  11814. */
  11815. begin tran
  11816.  
  11817. /* Local Smallmoney */
  11818. insert into spt_provider_types values
  11819.     (
  11820.     122 /*SQLMONEY4*/,      /* ss_dtype */
  11821.     4,              /* fixlen */
  11822.     'smallmoney',           /* type_name */
  11823.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  11824.     0,              /* best_match */
  11825.     null,               /* is_nullable */
  11826.     0,              /* case_sensitive */
  11827.     1,              /* fixed_prec_scale */
  11828.     0,              /* is_long */
  11829.     0,              /* auto_unique_value */
  11830.     10,             /* data_precision */
  11831.     null,               /* numeric_scale */
  11832.     null,               /* column_size */
  11833.     '$',                /* literal_prefix */
  11834.     null,               /* literal_suffix */
  11835.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  11836.     0,              /* unsigned_attribute */
  11837.     'smallmoney'            /* local_type_name */
  11838.     )   
  11839.     
  11840. /* Local Money */
  11841. insert into spt_provider_types values
  11842.     (
  11843.     60 /*SQLMONEY*/,        /* ss_dtype */
  11844.     8,              /* fixlen */
  11845.     'money',            /* type_name */
  11846.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  11847.     1,              /* best_match */
  11848.     null,               /* is_nullable */
  11849.     0,              /* case_sensitive */
  11850.     1,              /* fixed_prec_scale */
  11851.     0,              /* is_long */
  11852.     0,              /* auto_unique_value */
  11853.     19,             /* data_precision */
  11854.     null,               /* numeric_scale */
  11855.     null,               /* column_size */
  11856.     '$',                /* literal_prefix */
  11857.     null,               /* literal_suffix */
  11858.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  11859.     0,              /* unsigned_attribute */
  11860.     'money'             /* local_type_name */
  11861.     )   
  11862.  
  11863. if (charindex('7.00', @@version) = 0 and
  11864.     charindex('8.00', @@version) = 0)
  11865.     begin   /*  Add nullable type for non-Sphinx server */
  11866.  
  11867.     /* Local Moneyn */
  11868.     insert into spt_provider_types values
  11869.         (
  11870.         110 /*SQLMONEYN*/,  /* ss_dtype */
  11871.         4,          /* fixlen */
  11872.         'smallmoney',       /* type_name */
  11873.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  11874.         0,          /* best_match */
  11875.         null,           /* is_nullable */
  11876.         0,          /* case_sensitive */
  11877.         1,          /* fixed_prec_scale */
  11878.         0,          /* is_long */
  11879.         0,          /* auto_unique_value */
  11880.         10,         /* data_precision */
  11881.         null,           /* numeric_scale */
  11882.         null,           /* column_size */
  11883.         '$',            /* literal_prefix */
  11884.         null,           /* literal_suffix */
  11885.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  11886.         0,          /* unsigned_attribute */
  11887.         'smallmoney'        /* local_type_name */
  11888.         )   
  11889.     insert into spt_provider_types values
  11890.         (
  11891.         110 /*SQLMONEYN*/,  /* ss_dtype */
  11892.         8,          /* fixlen */
  11893.         'money',        /* type_name */
  11894.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  11895.         0,          /* best_match */
  11896.         null,           /* is_nullable */
  11897.         0,          /* case_sensitive */
  11898.         1,          /* fixed_prec_scale */
  11899.         0,          /* is_long */
  11900.         0,          /* auto_unique_value */
  11901.         19,         /* data_precision */
  11902.         null,           /* numeric_scale */
  11903.         null,           /* column_size */
  11904.         '$',            /* literal_prefix */
  11905.         null,           /* literal_suffix */
  11906.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  11907.         null,           /* unsigned_attribute */
  11908.         'money'         /* local_type_name */
  11909.         )   
  11910.     end
  11911.  
  11912. commit tran
  11913. go
  11914. dump tran master with no_log
  11915. go
  11916.  
  11917. /*
  11918. ** Insert the spt_provider_types rows for the numeric data types
  11919. */
  11920. begin tran
  11921.  
  11922. /* Local Float */
  11923. insert into spt_provider_types values
  11924.     (
  11925.     62 /*SQLFLT8*/,         /* ss_dtype */
  11926.     8,              /* fixlen */
  11927.     'float',            /* type_name */
  11928.     5 /*DBTYPE_R8*/,        /* oledb_data_type */
  11929.     1,              /* best_match */
  11930.     null,               /* is_nullable */
  11931.     0,              /* case_sensitive */
  11932.     0,              /* fixed_prec_scale */
  11933.     0,              /* is_long */
  11934.     0,              /* auto_unique_value */
  11935.     15,             /* data_precision */
  11936.     null,               /* numeric_scale */
  11937.     null,               /* column_size */
  11938.     null,               /* literal_prefix */
  11939.     null,               /* literal_suffix */
  11940.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  11941.     0,              /* unsigned_attribute */
  11942.     'float'             /* local_type_name */
  11943.     )   
  11944.  
  11945. /* Local Real */
  11946. insert into spt_provider_types values
  11947.     (
  11948.     59 /*SQLFLT4*/,         /* ss_dtype */
  11949.     4,              /* fixlen */
  11950.     'real',             /* type_name */
  11951.     4 /*DBTYPE_R4*/,        /* oledb_data_type */
  11952.     1,              /* best_match */
  11953.     null,               /* is_nullable */
  11954.     0,              /* case_sensitive */
  11955.     0,              /* fixed_prec_scale */
  11956.     0,              /* is_long */
  11957.     0,              /* auto_unique_value */
  11958.     7,              /* data_precision */
  11959.     null,               /* numeric_scale */
  11960.     null,               /* column_size */
  11961.     null,               /* literal_prefix */
  11962.     null,               /* literal_suffix */
  11963.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  11964.     0,              /* unsigned_attribute */
  11965.     'real'              /* local_type_name */
  11966.     )   
  11967.  
  11968. /* Local Int */
  11969. insert into spt_provider_types values
  11970.     (
  11971.     56 /*SQLINT4*/,         /* ss_dtype */
  11972.     4,              /* fixlen */
  11973.     'int',              /* type_name */
  11974.     3 /*DBTYPE_I4*/,        /* oledb_data_type */
  11975.     1,              /* best_match */
  11976.     null,               /* is_nullable */
  11977.     0,              /* case_sensitive */
  11978.     1,              /* fixed_prec_scale */
  11979.     0,              /* is_long */
  11980.     1,              /* auto_unique_value */
  11981.     10,             /* data_precision */
  11982.     null,               /* numeric_scale */
  11983.     null,               /* column_size */
  11984.     null,               /* literal_prefix */
  11985.     null,               /* literal_suffix */
  11986.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  11987.     0,              /* unsigned_attribute */
  11988.     'int'               /* local_type_name */
  11989.     )   
  11990.  
  11991. /* Local Smallint */
  11992. insert into spt_provider_types values
  11993.     (
  11994.     52 /*SQLINT2*/,         /* ss_dtype */
  11995.     2,              /* fixlen */
  11996.     'smallint',         /* type_name */
  11997.     2 /*DBTYPE_I2*/,        /* oledb_data_type */
  11998.     1,              /* best_match */
  11999.     null,               /* is_nullable */
  12000.     0,              /* case_sensitive */
  12001.     1,              /* fixed_prec_scale */
  12002.     0,              /* is_long */
  12003.     1,              /* auto_unique_value */
  12004.     5,              /* data_precision */
  12005.     null,               /* numeric_scale */
  12006.     null,               /* column_size */
  12007.     null,               /* literal_prefix */
  12008.     null,               /* literal_suffix */
  12009.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12010.     0,              /* unsigned_attribute */
  12011.     'smallint'          /* local_type_name */
  12012.     )   
  12013.  
  12014. /* Local Tinyint */
  12015. insert into spt_provider_types values
  12016.     (
  12017.     48 /*SQLINT1*/,         /* ss_dtype */
  12018.     1,              /* fixlen */
  12019.     'tinyint',          /* type_name */
  12020.     17 /*DBTYPE_UI1*/,      /* oledb_data_type */
  12021.     1,              /* best_match */
  12022.     null,               /* is_nullable */
  12023.     0,              /* case_sensitive */
  12024.     1,              /* fixed_prec_scale */
  12025.     0,              /* is_long */
  12026.     1,              /* auto_unique_value */
  12027.     3,              /* data_precision */
  12028.     null,               /* numeric_scale */
  12029.     null,               /* column_size */
  12030.     null,               /* literal_prefix */
  12031.     null,               /* literal_suffix */
  12032.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12033.     1,              /* unsigned_attribute */
  12034.     'tinyint'           /* local_type_name */
  12035.     )   
  12036. commit tran
  12037. go
  12038. dump tran master with no_log
  12039. go
  12040. begin tran  
  12041. if (charindex('6.00', @@version) > 0 or
  12042.     charindex('6.50', @@version) > 0 or
  12043.     charindex('7.00', @@version) > 0 or
  12044.     charindex('8.00', @@version) > 0)
  12045.     begin   /*  Add 6.0 data types */
  12046.     
  12047.     /* Local Decimal */
  12048.     insert into spt_provider_types values
  12049.         (
  12050.         55 /*SQLDECIMAL*/,      /* ss_dtype */
  12051.         0,              /* fixlen */
  12052.         'decimal',          /* type_name */
  12053.         131 /*DBTYPE_NUMERIC*/,     /* oledb_data_type */
  12054.         0,              /* best_match */
  12055.         null,               /* is_nullable */
  12056.         0,              /* case_sensitive */
  12057.         1,              /* fixed_prec_scale */
  12058.         0,              /* is_long */
  12059.         1,              /* auto_unique_value */
  12060.         38,             /* data_precision */
  12061.         null,               /* numeric_scale */
  12062.         null,               /* column_size */
  12063.         null,               /* literal_prefix */
  12064.         null,               /* literal_suffix */
  12065.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12066.         0,              /* unsigned_attribute */
  12067.         'decimal'           /* local_type_name */
  12068.         )   
  12069.  
  12070.     /* Local Numeric */
  12071.     insert into spt_provider_types values
  12072.         (
  12073.         63 /*SQLNUMERIC*/,      /* ss_dtype */
  12074.         0,              /* fixlen */
  12075.         'numeric',          /* type_name */
  12076.         131 /*DBTYPE_NUMERIC*/,     /* oledb_data_type */
  12077.         1,              /* best_match */
  12078.         null,               /* is_nullable */
  12079.         0,              /* case_sensitive */
  12080.         1,              /* fixed_prec_scale */
  12081.         0,              /* is_long */
  12082.         1,              /* auto_unique_value */
  12083.         38,             /* data_precision */
  12084.         null,               /* numeric_scale */
  12085.         null,               /* column_size */
  12086.         null,               /* literal_prefix */
  12087.         null,               /* literal_suffix */
  12088.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12089.         0,              /* unsigned_attribute */
  12090.         'numeric'           /* local_type_name */
  12091.         )   
  12092.     end
  12093. commit tran
  12094. go
  12095. dump tran master with no_log
  12096. go
  12097. begin tran
  12098. if (charindex('7.00', @@version) = 0 and
  12099.     charindex('8.00', @@version) = 0)
  12100.     begin   /*  Add nullable type for non-Sphinx server */
  12101.     
  12102.     /* Local Floatn */
  12103.     insert into spt_provider_types values
  12104.         (
  12105.         109 /*SQLFLTN*/,        /* ss_dtype */
  12106.         8,              /* fixlen */
  12107.         'float',            /* type_name */
  12108.         5 /*DBTYPE_R8*/,        /* oledb_data_type */
  12109.         0,              /* best_match */
  12110.         null,               /* is_nullable */
  12111.         0,              /* case_sensitive */
  12112.         0,              /* fixed_prec_scale */
  12113.         0,              /* is_long */
  12114.         0,              /* auto_unique_value */
  12115.         15,             /* data_precision */
  12116.         null,               /* numeric_scale */
  12117.         null,               /* column_size */
  12118.         null,               /* literal_prefix */
  12119.         null,               /* literal_suffix */
  12120.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12121.         null,               /* unsigned_attribute */
  12122.         'float'             /* local_type_name */
  12123.         )   
  12124.     insert into spt_provider_types values
  12125.         (
  12126.         109 /*SQLFLT4*/,        /* ss_dtype */
  12127.         4,              /* fixlen */
  12128.         'real',             /* type_name */
  12129.         4 /*DBTYPE_R4*/,        /* oledb_data_type */
  12130.         0,              /* best_match */
  12131.         null,               /* is_nullable */
  12132.         0,              /* case_sensitive */
  12133.         0,              /* fixed_prec_scale */
  12134.         0,              /* is_long */
  12135.         0,              /* auto_unique_value */
  12136.         7,              /* data_precision */
  12137.         null,               /* numeric_scale */
  12138.         null,               /* column_size */
  12139.         null,               /* literal_prefix */
  12140.         null,               /* literal_suffix */
  12141.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12142.         null,               /* unsigned_attribute */
  12143.         'real'              /* local_type_name */
  12144.         )   
  12145.  
  12146.     /* Local Intn */
  12147.     insert into spt_provider_types values
  12148.         (
  12149.         38 /*SQLINTN*/,         /* ss_dtype */
  12150.         4,              /* fixlen */
  12151.         'int',              /* type_name */
  12152.         3 /*DBTYPE_I4*/,        /* oledb_data_type */
  12153.         1,              /* best_match */
  12154.         null,               /* is_nullable */
  12155.         0,              /* case_sensitive */
  12156.         1,              /* fixed_prec_scale */
  12157.         0,              /* is_long */
  12158.         1,              /* auto_unique_value */
  12159.         10,             /* data_precision */
  12160.         null,               /* numeric_scale */
  12161.         null,               /* column_size */
  12162.         null,               /* literal_prefix */
  12163.         null,               /* literal_suffix */
  12164.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12165.         0,              /* unsigned_attribute */
  12166.         'int'               /* local_type_name */
  12167.         )   
  12168.     insert into spt_provider_types values
  12169.         (
  12170.         38 /*SQLINTN*/,         /* ss_dtype */
  12171.         2,              /* fixlen */
  12172.         'smallint',         /* type_name */
  12173.         2 /*DBTYPE_I2*/,        /* oledb_data_type */
  12174.         0,              /* best_match */
  12175.         null,               /* is_nullable */
  12176.         0,              /* case_sensitive */
  12177.         1,              /* fixed_prec_scale */
  12178.         0,              /* is_long */
  12179.         1,              /* auto_unique_value */
  12180.         5,              /* data_precision */
  12181.         null,               /* numeric_scale */
  12182.         null,               /* column_size */
  12183.         null,               /* literal_prefix */
  12184.         null,               /* literal_suffix */
  12185.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12186.         0,              /* unsigned_attribute */
  12187.         'smallint'          /* local_type_name */
  12188.         )   
  12189.     insert into spt_provider_types values
  12190.         (
  12191.         38 /*SQLINTN*/,         /* ss_dtype */
  12192.         1,              /* fixlen */
  12193.         'tinyint',          /* type_name */
  12194.         17 /*DBTYPE_UI1*/,      /* oledb_data_type */
  12195.         0,              /* best_match */
  12196.         null,               /* is_nullable */
  12197.         0,              /* case_sensitive */
  12198.         1,              /* fixed_prec_scale */
  12199.         0,              /* is_long */
  12200.         1,              /* auto_unique_value */
  12201.         3,              /* data_precision */
  12202.         null,               /* numeric_scale */
  12203.         null,               /* column_size */
  12204.         null,               /* literal_prefix */
  12205.         null,               /* literal_suffix */
  12206.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12207.         1,              /* unsigned_attribute */
  12208.         'tinyint'           /* local_type_name */
  12209.         )   
  12210.  
  12211.     if (charindex('6.00', @@version) > 0 or charindex('6.50', @@version) > 0)
  12212.         begin   /*  Add 6.0 data types */
  12213.         
  12214.         /* Local Decimaln */
  12215.         insert into spt_provider_types values
  12216.             (
  12217.             106 /*SQLDECIMALN*/,        /* ss_dtype */
  12218.             0,              /* fixlen */
  12219.             'decimal',          /* type_name */
  12220.             131 /*DBTYPE_NUMERIC*/,     /* oledb_data_type */
  12221.             0,              /* best_match */
  12222.             null,               /* is_nullable */
  12223.             0,              /* case_sensitive */
  12224.             1,              /* fixed_prec_scale */
  12225.             0,              /* is_long */
  12226.             1,              /* auto_unique_value */
  12227.             38,             /* data_precision */
  12228.             null,               /* numeric_scale */
  12229.             null,               /* column_size */
  12230.             null,               /* literal_prefix */
  12231.             null,               /* literal_suffix */
  12232.             3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12233.             0,              /* unsigned_attribute */
  12234.             'decimal'           /* local_type_name */
  12235.             )   
  12236.  
  12237.         /* Local Numericn */
  12238.         insert into spt_provider_types values
  12239.             (
  12240.             108 /*SQLNUMERICN*/,        /* ss_dtype */
  12241.             0,              /* fixlen */
  12242.             'numeric',          /* type_name */
  12243.             131 /*DBTYPE_NUMERIC*/,     /* oledb_data_type */
  12244.             0,              /* best_match */
  12245.             null,               /* is_nullable */
  12246.             0,              /* case_sensitive */
  12247.             1,              /* fixed_prec_scale */
  12248.             0,              /* is_long */
  12249.             1,              /* auto_unique_value */
  12250.             38,             /* data_precision */
  12251.             null,               /* numeric_scale */
  12252.             null,               /* column_size */
  12253.             null,               /* literal_prefix */
  12254.             null,               /* literal_suffix */
  12255.             3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12256.             0,              /* unsigned_attribute */
  12257.             'numeric'           /* local_type_name */
  12258.             )   
  12259.         end
  12260.     end
  12261. commit tran
  12262. go
  12263. dump tran master with no_log
  12264. go
  12265.  
  12266. /*
  12267. **  Remaining data types
  12268. */
  12269. if (charindex('7.00', @@version) = 0 and
  12270.     charindex('8.00', @@version) = 0)
  12271. begin
  12272.     print ''
  12273.     print ''
  12274.     print 'Warning:'
  12275.     print 'you are installing the stored procedures '
  12276.     print 'on a pre 8.0 SQL Server.'
  12277.     print 'Ignore the following errors.'
  12278. end
  12279. go
  12280. begin tran
  12281.  
  12282. if (charindex('7.00', @@version) > 0 or
  12283.     charindex('8.00', @@version) > 0)
  12284.     begin
  12285.     /* Local Bit */
  12286.     insert into spt_provider_types values
  12287.         (
  12288.         50 /*SQLBIT*/,          /* ss_dtype */
  12289.         0,              /* fixlen */
  12290.         'bit',              /* type_name */
  12291.         11 /*DBTYPE_BOOL*/,     /* oledb_data_type */
  12292.         1,              /* best_match */
  12293.         null,               /* is_nullable */
  12294.         0,              /* case_sensitive */
  12295.         0,              /* fixed_prec_scale */
  12296.         0,              /* is_long */
  12297.         0,              /* auto_unique_value */
  12298.         1,              /* data_precision */
  12299.         null,               /* numeric_scale */
  12300.         null,               /* column_size */
  12301.         null,               /* literal_prefix */
  12302.         null,               /* literal_suffix */
  12303.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12304.         null,               /* unsigned_attribute */
  12305.         'bit'               /* local_type_name */
  12306.         )       
  12307.     
  12308.     /* Local Timestamp */
  12309.     insert into spt_provider_types values
  12310.         (
  12311.         0,              /* ss_dtype */
  12312.         8,              /* fixlen */
  12313.         'timestamp',            /* type_name */
  12314.         128 /*DBTYPE_BYTES*/,       /* oledb_data_type */
  12315.         0,              /* best_match */
  12316.         null,               /* is_nullable */
  12317.         0,              /* case_sensitive */
  12318.         0,              /* fixed_prec_scale */
  12319.         0,              /* is_long */
  12320.         0,              /* auto_unique_value */
  12321.         null,               /* data_precision */
  12322.         null,               /* numeric_scale */
  12323.         null,               /* column_size */
  12324.         '0x',               /* literal_prefix */
  12325.         null,               /* literal_suffix */
  12326.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12327.         null,               /* unsigned_attribute */
  12328.         'timestamp'         /* local_type_name */
  12329.         )   
  12330.     /* Local GUID */
  12331.     insert into spt_provider_types values
  12332.         (
  12333.         0,              /* ss_dtype */
  12334.         16,             /* fixlen */
  12335.         'uniqueidentifier',     /* type_name */
  12336.         72 /*DBTYPE_GUID*/,     /* oledb_data_type */
  12337.         1,              /* best_match */
  12338.         null,               /* is_nullable */
  12339.         0,              /* case_sensitive */
  12340.         0,              /* fixed_prec_scale */
  12341.         0,              /* is_long */
  12342.         0,              /* auto_unique_value */
  12343.         null,               /* data_precision */
  12344.         null,               /* numeric_scale */
  12345.         null,               /* column_size */
  12346.         '''',               /* literal_prefix */
  12347.         '''',               /* literal_suffix */
  12348.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12349.         null,               /* unsigned_attribute */
  12350.         'uniqueidentifier'      /* local_type_name */
  12351.         )   
  12352.     
  12353.     /* Get case sensitivity */
  12354.     declare @ncase_sensitive bit
  12355.     select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  12356.  
  12357.     /* Local NChar */
  12358.     insert into spt_provider_types values
  12359.         (
  12360.         0,              /* ss_dtype */
  12361.         0,              /* fixlen */
  12362.         'nchar',            /* type_name */
  12363.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  12364.         0,              /* best_match */
  12365.         null,               /* is_nullable */
  12366.         @ncase_sensitive,       /* case_sensitive */
  12367.         0,              /* fixed_prec_scale */
  12368.         0,              /* is_long */
  12369.         0,              /* auto_unique_value */
  12370.         null,               /* data_precision */
  12371.         null,               /* numeric_scale */
  12372.         null,               /* column_size */
  12373.         'N''',              /* literal_prefix */
  12374.         '''',               /* literal_suffix */
  12375.         4 /*DB_SEARCHABLE*/,        /* searchable */
  12376.         null,               /* unsigned_attribute */
  12377.         'nchar'             /* local_type_name */
  12378.         )   
  12379.  
  12380.     /* Local NVarChar */
  12381.     insert into spt_provider_types values
  12382.         (
  12383.         0,              /* ss_dtype */
  12384.         null,               /* fixlen */
  12385.         'nvarchar',         /* type_name */
  12386.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  12387.         1,              /* best_match */
  12388.         null,               /* is_nullable */
  12389.         @ncase_sensitive,       /* case_sensitive */
  12390.         0,              /* fixed_prec_scale */
  12391.         0,              /* is_long */
  12392.         0,              /* auto_unique_value */
  12393.         null,               /* data_precision */
  12394.         null,               /* numeric_scale */
  12395.         null,               /* column_size */
  12396.         'N''',              /* literal_prefix */
  12397.         '''',               /* literal_suffix */
  12398.         4 /*DB_SEARCHABLE*/,        /* searchable */
  12399.         null,               /* unsigned_attribute */
  12400.         'nvarchar'          /* local_type_name */
  12401.         )   
  12402.  
  12403.     /* Local NText */
  12404.     insert into spt_provider_types values
  12405.         (
  12406.         0,              /* ss_dtype */
  12407.         null,               /* fixlen */
  12408.         'ntext',            /* type_name */
  12409.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  12410.         0,              /* best_match */
  12411.         null,               /* is_nullable */
  12412.         @ncase_sensitive,       /* case_sensitive */
  12413.         0,              /* fixed_prec_scale */
  12414.         1,              /* is_long */
  12415.         0,              /* auto_unique_value */
  12416.         null,               /* data_precision */
  12417.         null,               /* numeric_scale */
  12418.         1073741823,         /* column_size */
  12419.         'N''',              /* literal_prefix */
  12420.         '''',               /* literal_suffix */
  12421.         2 /*DB_LIKE_ONLY*/,     /* searchable */
  12422.         null,               /* unsigned_attribute */
  12423.         'ntext'             /* local_type_name */
  12424.         )   
  12425.  
  12426.     if (charindex('8.00', @@version) > 0)
  12427.     begin
  12428.     /* Local BIGINT */
  12429.     insert into spt_provider_types values
  12430.         (
  12431.         127,                /* ss_dtype */
  12432.         8,              /* fixlen */
  12433.         'bigint',           /* type_name */
  12434.         20 /*DBTYPE_I8*/,       /* oledb_data_type */
  12435.         1,              /* best_match */
  12436.         null,               /* is_nullable */
  12437.         0,              /* case_sensitive */
  12438.         1,              /* fixed_prec_scale */
  12439.         0,              /* is_long */
  12440.         1,              /* auto_unique_value */
  12441.         19,             /* data_precision */
  12442.         null,           /* numeric_scale */
  12443.         null,           /* column_size */
  12444.         null,           /* literal_prefix */
  12445.         null,           /* literal_suffix */
  12446.         3 /*DB_ALL_EXCEPT_LIKE*/,       /* searchable */
  12447.         0,              /* unsigned_attribute */
  12448.         'bigint'        /* local_type_name */
  12449.         )
  12450.  
  12451.     /* sql_variant */
  12452.     insert into spt_provider_types values
  12453.         (
  12454.         98,             /* ss_dtype */
  12455.         null,           /* fixlen */
  12456.         'sql_variant',  /* type_name */
  12457.         12 /*DBTYPE_VARIANT*/,      /* oledb_data_type */
  12458.         1,              /* best_match */
  12459.         null,           /* is_nullable */
  12460.         0,              /* case_sensitive */
  12461.         0,              /* fixed_prec_scale */
  12462.         0,              /* is_long */
  12463.         0,              /* auto_unique_value */
  12464.         16,             /* data_precision sizeof(VARIANT)*/
  12465.         null,           /* numeric_scale */
  12466.         null,           /* column_size */
  12467.         null,           /* literal_prefix */
  12468.         null,           /* literal_suffix */
  12469.         3 ,                 /*DB_ALL_EXCEPT_LIKE*/
  12470.         null,           /* unsigned_attribute */
  12471.         'sql_variant'   /* local_type_name */
  12472.         )
  12473.     end
  12474.  
  12475.  
  12476.     /* data types changed in Sphinx: DO AFTER ALL TYPES INSERTED! */    
  12477.     update spt_provider_types set ss_dtype = (select xtype from systypes    where type_name like name+'%')
  12478.  
  12479.     end
  12480. commit tran
  12481. go
  12482. dump tran master with no_log
  12483. go
  12484. begin tran
  12485. if (charindex('7.00', @@version) = 0 and
  12486.     charindex('8.00', @@version) = 0)
  12487.     begin
  12488.     /* Local Bit */
  12489.     insert into spt_provider_types values
  12490.         (
  12491.         50 /*SQLBIT*/,          /* ss_dtype */
  12492.         0,              /* fixlen */
  12493.         'bit',              /* type_name */
  12494.         11 /*DBTYPE_BOOL*/,     /* oledb_data_type */
  12495.         1,              /* best_match */
  12496.         0,              /* is_nullable */
  12497.         0,              /* case_sensitive */
  12498.         0,              /* fixed_prec_scale */
  12499.         0,              /* is_long */
  12500.         0,              /* auto_unique_value */
  12501.         1,              /* data_precision */
  12502.         null,               /* numeric_scale */
  12503.         null,               /* column_size */
  12504.         null,               /* literal_prefix */
  12505.         null,               /* literal_suffix */
  12506.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12507.         null,               /* unsigned_attribute */
  12508.         'bit'               /* local_type_name */
  12509.         )   
  12510.  
  12511.     /* Local Timestamp */
  12512.     insert into spt_provider_types values
  12513.         (
  12514.         45 /*SQLBINARY*/,       /* ss_dtype */
  12515.         8,              /* fixlen */
  12516.         'timestamp',            /* type_name */
  12517.         128 /*DBTYPE_BYTES*/,       /* oledb_data_type */
  12518.         0,              /* best_match */
  12519.         null,               /* is_nullable */
  12520.         0,              /* case_sensitive */
  12521.         0,              /* fixed_prec_scale */
  12522.         0,              /* is_long */
  12523.         0,              /* auto_unique_value */
  12524.         null,               /* data_precision */
  12525.         null,               /* numeric_scale */
  12526.         null,               /* column_size */
  12527.         '0x',               /* literal_prefix */
  12528.         null,               /* literal_suffix */
  12529.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12530.         null,               /* unsigned_attribute */
  12531.         'timestamp'         /* local_type_name */
  12532.         )       
  12533. end
  12534.  
  12535. commit tran
  12536. go
  12537. dump tran master with no_log
  12538. go
  12539.  
  12540. create unique clustered index datatypeinfoclust on spt_provider_types(ss_dtype,fixlen)
  12541. go
  12542.  
  12543. dump tran master with no_log
  12544. go
  12545. if (charindex('6.00', @@version) > 0)
  12546.     begin
  12547.     if (exists (select * from sysobjects
  12548.         where name = 'spt_provider_types' and type = 'U '))
  12549.         begin
  12550.         drop table spt_provider_types
  12551.         dump tran master with no_log
  12552.         end
  12553.     end
  12554. go
  12555.  
  12556. if (charindex('7.00', @@version) = 0 and
  12557.     charindex('8.00', @@version) = 0)
  12558. begin
  12559.     print ''
  12560.     print ''
  12561.     print 'Warning:'
  12562.     print 'you are installing the stored procedures '
  12563.     print 'on a pre 8.0 SQL Server.'
  12564.     print 'Ignore the following errors.'
  12565. end
  12566. go
  12567.  
  12568.  
  12569. /* 
  12570. ** (2/11/98) 
  12571. ** The following SP is shared by instcat.sql and the server to add information
  12572. ** related to server language collations. The server calls it at the end of
  12573. ** collation change to update the catalog with the new language collations
  12574. ** Everything in this SP should NOT use tempdb.  
  12575. */
  12576.  
  12577. create procedure sp_add_server_sortinfo
  12578. as
  12579. -- spt_server_info has unigue clustered index on attribute_id
  12580. delete spt_server_info where attribute_id in (16,18)
  12581.  
  12582. --insert into spt_server_info
  12583. --  select 18,'COLLATION_SEQ',
  12584. --      'charset='+t2.name+' sort_order='+t1.name
  12585. --      +' charset_num='+rtrim(convert(char(4),t1.csid))+
  12586. --      ' sort_order_num='+rtrim(convert(char(4),t1.id))
  12587. --  from master.dbo.syscharsets t1, master.dbo.syscharsets t2, master.dbo.sysconfigures t3
  12588. --  where t1.csid=t2.id and t1.id=t3.value and t3.config in (123,1123)
  12589. declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
  12590. declare @case_sensitive bit
  12591. declare @ncase_sensitive bit
  12592. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  12593. select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  12594. select @sortid = value from sysconfigures where config = 1123
  12595. select @csid = csid, @sortname = name from master.dbo.syscharsets where id = @sortid
  12596. select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
  12597.         + ' charset_num=' + rtrim(convert(char(4),@csid))+
  12598.         ' sort_order_num=' + rtrim(convert(char(4),@sortid))
  12599.         from syscharsets where id = @csid
  12600.  
  12601. insert into spt_server_info
  12602.         values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))
  12603.  
  12604. if @case_sensitive = 1 /* If case sensitive server */
  12605. begin
  12606.     insert into spt_server_info
  12607.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  12608. end
  12609. else
  12610. begin
  12611.     insert into spt_server_info
  12612.         values (16,'IDENTIFIER_CASE','MIXED')
  12613. end
  12614.  
  12615.  
  12616. update spt_datatype_info set CASE_SENSITIVE = @case_sensitive 
  12617.     where DATA_TYPE in (-1, 1, 12) /* CHAR types */
  12618. update spt_datatype_info set CASE_SENSITIVE = @ncase_sensitive
  12619.     where DATA_TYPE in (-10, -9, -8) /* NCHAR types */
  12620. update spt_provider_types set case_sensitive = @case_sensitive 
  12621.     where oledb_data_type = 129 /*DBTYPE_STR*/
  12622. update spt_provider_types set case_sensitive = @ncase_sensitive
  12623.     where oledb_data_type = 130 /*DBTYPE_WSTR*/
  12624. /* sp_add_server_sortinfo */
  12625. go  
  12626.  
  12627.  
  12628. if (charindex('7.00', @@version) > 0)
  12629. begin
  12630.     exec sp_add_server_sortinfo
  12631. end
  12632. go
  12633.  
  12634. -- 8.0 version of sp_add_server_sortinfo
  12635. --
  12636. create procedure sp_add_server_sortinfo75
  12637. as
  12638. -- spt_server_info has unigue clustered index on attribute_id
  12639.     delete spt_server_info where attribute_id in (16, 18)
  12640.  
  12641.     declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
  12642.     declare @case_sensitive bit
  12643.     declare @ncase_sensitive bit
  12644.     select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  12645.     select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  12646.  
  12647.     select @sortid = convert(int, ServerProperty('sqlsortorder')) 
  12648.     select @csid = convert(int, ServerProperty('sqlcharset')) 
  12649.     select @sortname = name from master.dbo.syscharsets where id = @sortid
  12650.  
  12651.     if @sortid = 0 -- Non-SQL Collations
  12652.         BEGIN
  12653.         select @attribute_value = 'charset=' 
  12654.             + name
  12655.             + ' collation=' + isnull(convert(sysname, ServerProperty('collation')), ' ')
  12656.             from syscharsets where id = @csid
  12657.         END
  12658.     else
  12659.         BEGIN
  12660.         select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
  12661.             + ' charset_num=' + rtrim(convert(char(4),@csid))+
  12662.             ' sort_order_num=' + rtrim(convert(char(4),@sortid))
  12663.             from syscharsets where id = @csid
  12664.         END
  12665.  
  12666.     insert into spt_server_info
  12667.             values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))
  12668.  
  12669.     if @case_sensitive = 1 /* If case sensitive server */
  12670.     begin
  12671.         insert into spt_server_info
  12672.             values (16,'IDENTIFIER_CASE','SENSITIVE')
  12673.     end
  12674.     else
  12675.     begin
  12676.         insert into spt_server_info
  12677.             values (16,'IDENTIFIER_CASE','MIXED')
  12678.     end
  12679.  
  12680.  
  12681.     update spt_datatype_info set CASE_SENSITIVE = @case_sensitive 
  12682.         where DATA_TYPE in (-1, 1, 12) /* CHAR types */
  12683.     update spt_datatype_info set CASE_SENSITIVE = @ncase_sensitive
  12684.         where DATA_TYPE in (-10, -9, -8) /* NCHAR types */
  12685.     update spt_provider_types set case_sensitive = @case_sensitive 
  12686.         where oledb_data_type = 129 /*DBTYPE_STR*/
  12687.     update spt_provider_types set case_sensitive = @ncase_sensitive
  12688.         where oledb_data_type = 130 /*DBTYPE_WSTR*/
  12689. /* sp_add_server_info75 */
  12690. go 
  12691.  
  12692.  
  12693.  
  12694. if (charindex('8.00', @@version) > 0)
  12695. begin
  12696.     exec sp_add_server_sortinfo75
  12697. end
  12698. go
  12699.  
  12700. print ''
  12701. print 'creating sp_catalogs_rowset'
  12702. go
  12703.  
  12704. create procedure sp_catalogs_rowset
  12705.     (
  12706.     @catalog_name       varchar(255)
  12707.     )       
  12708. as
  12709.     select
  12710.         CATALOG_NAME    = name,
  12711.         DESCRIPTION = convert(varchar(1),null)
  12712.     from    master.dbo.sysdatabases
  12713.     where   name = @catalog_name AND has_dbaccess(name)=1
  12714. go
  12715. dump tran master with no_log
  12716. go
  12717. create procedure sp_catalogs_rowset;2
  12718.     (
  12719.     @dummy      int /* remove when Hydra bug 17032 is fixed */
  12720.     )
  12721. as
  12722.     select
  12723.         CATALOG_NAME    = name,
  12724.         DESCRIPTION = convert(varchar(1),null)
  12725.     from    master.dbo.sysdatabases
  12726.     where   has_dbaccess(name)=1
  12727.     order by 1
  12728. go
  12729. dump tran master with no_log
  12730. go
  12731.  
  12732. if (charindex('7.00', @@version) = 0 and
  12733.     charindex('8.00', @@version) = 0)
  12734. begin
  12735.     print ''
  12736.     print ''
  12737.     print 'Warning:'
  12738.     print 'you are installing the stored procedures '
  12739.     print 'on a pre 8.0 SQL Server.'
  12740.     print 'Ignore the following errors.'
  12741. end
  12742. else
  12743.     drop proc sp_catalogs_rowset
  12744. go
  12745.  
  12746. /*  Procedure for 8.0 server */
  12747. create procedure sp_catalogs_rowset
  12748.     (
  12749.     @catalog_name       sysname
  12750.     )       
  12751. as
  12752.     select
  12753.         CATALOG_NAME    = name,
  12754.         DESCRIPTION = convert(nvarchar(1),null)
  12755.     from    master.dbo.sysdatabases
  12756.     where   name = @catalog_name AND has_dbaccess(name)=1
  12757. go
  12758. dump tran master with no_log
  12759. go
  12760. create procedure sp_catalogs_rowset;2
  12761.     (
  12762.     @dummy      int /* remove when Hydra bug 17032 is fixed */
  12763.     )
  12764. as
  12765.     select
  12766.         CATALOG_NAME    = name,
  12767.         DESCRIPTION = convert(nvarchar(1),null)
  12768.     from    master.dbo.sysdatabases
  12769.     where   has_dbaccess(name)=1
  12770.     order by 1
  12771. go
  12772. dump tran master with no_log
  12773. go
  12774. create procedure sp_catalogs_rowset;5
  12775.     (
  12776.     @server_name    sysname,
  12777.     @catalog_name   sysname = NULL
  12778.     )
  12779. as
  12780.     select  CATALOG_NAME,
  12781.         DESCRIPTION
  12782.     from master.dbo.SYSREMOTE_CATALOGS < @server_name, @catalog_name >
  12783.     where   has_dbaccess(CATALOG_NAME)=1
  12784.     order by 1
  12785. go
  12786.  
  12787. grant execute on sp_catalogs_rowset to public
  12788. go
  12789.  
  12790. dump tran master with no_log
  12791. go
  12792. if (charindex('6.00', @@version) > 0)
  12793.     begin
  12794.     if (exists (select * from sysobjects
  12795.             where name = 'sp_catalogs_rowset' and type = 'P '))
  12796.         begin
  12797.         drop procedure sp_catalogs_rowset
  12798.         dump tran master with no_log
  12799.         end
  12800.     end
  12801. go
  12802.  
  12803.  
  12804. print ''
  12805. print 'creating sp_column_privileges_rowset'
  12806. go
  12807.  
  12808. /*  Procedure for 6.0 and 6.5 server */
  12809. CREATE PROCEDURE sp_column_privileges_rowset
  12810.     (
  12811.     @table_name     varchar(255) = null,
  12812.     @table_schema   varchar(255) = null,
  12813.     @column_name    varchar(255) = null,
  12814.     @grantor    varchar(255) = null,
  12815.     @grantee    varchar(255) = null
  12816.     )
  12817. as
  12818. IF @table_name is not null
  12819.     BEGIN
  12820.     select
  12821.         GRANTOR     = user_name(p.grantor),
  12822.         GRANTEE     = user_name(u.uid),
  12823.         TABLE_CATALOG   = db_name(),
  12824.         TABLE_SCHEMA    = user_name(o.uid),
  12825.         TABLE_NAME  = o.name,
  12826.         COLUMN_NAME = c.name,
  12827.         COLUMN_GUID = convert(binary(16),null),
  12828.         COLUMN_PROPID   = convert(int,null),
  12829.         PRIVILEGE_TYPE  = convert(varchar(30),
  12830.                     case p.action
  12831.                     when 193 then 'SELECT'
  12832.                     when 195 then 'INSERT'
  12833.                     when 197 then 'UPDATE'
  12834.                     else 'REFERENCES'
  12835.                     end),
  12836.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  12837.     from 
  12838.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  12839.     where
  12840.         o.name = @table_name
  12841.     and     o.type in ('U','V','S')
  12842.     and (@table_schema is null or @table_schema = user_name(o.uid))
  12843.     and c.id = o.id
  12844.     and     (@column_name is null or @column_name = c.name)
  12845.     and     c.id = p.id
  12846.     and (@grantor is null or @grantor = user_name(p.grantor))
  12847.     and     case 
  12848.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12849.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  12850.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  12851.             end & v.high <> 0           /* permission applies to this column */
  12852.     and     v.number <= (select count(*) from syscolumns where id = o.id)   /* ranges from 1 to # of columns in table */
  12853.     and     v.type = 'P'
  12854.     and     v.number = c.colid
  12855.     and (@grantee is null or @grantee = user_name(u.uid))
  12856.             /* expand groups */
  12857.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  12858.     and     p.protecttype <> 206    /* only grant rows */
  12859.     and     p.action in (26,193,195,197)
  12860.     and     o.uid <> u.uid          /* no rows for owner */
  12861.     and     not exists (            /* exclude revoke'd privileges */
  12862.             select *
  12863.             from sysprotects p1
  12864.             where
  12865.                 p1.protecttype = 206
  12866.             and     p1.action = p.action
  12867.             and     p1.id = p.id
  12868.             and     p1.uid = u.uid
  12869.             and     case 
  12870.                 when substring(p1.columns, 1, 1) & 1 is null then 255   /* all columns have permission */
  12871.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  12872.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  12873.                 end & v.high <> 0)          /* permission applies to this column */
  12874.     union
  12875.     select  /*  Add rows for table owner */
  12876.         GRANTOR     = user_name(u.uid),
  12877.         GRANTEE     = user_name(o.uid),
  12878.         TABLE_CATALOG   = db_name(),
  12879.         TABLE_SCHEMA    = user_name(o.uid),
  12880.         TABLE_NAME  = o.name,
  12881.         COLUMN_NAME = c.name,
  12882.         COLUMN_GUID = convert(binary(16),null),
  12883.         COLUMN_PROPID   = convert(int,null),
  12884.         PRIVILEGE_TYPE  = convert(varchar(30),
  12885.                     case v.number
  12886.                     when 193 then 'SELECT'
  12887.                     when 195 then 'INSERT'
  12888.                     when 197 then 'UPDATE'
  12889.                     else 'REFERENCES'
  12890.                     end),
  12891.         IS_GRANTABLE    = convert(bit,1)    
  12892.     from 
  12893.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  12894.     where
  12895.         o.name = @table_name
  12896.     and     o.type in ('U','V','S')
  12897.     and (@table_schema is null or @table_schema = user_name(o.uid))
  12898.     and (@grantee is null or @grantee = user_name(o.uid))
  12899.     and c.id = o.id
  12900.     and     (@column_name is null or @column_name = c.name)
  12901.     and     u.uid = 1       /* grantor is dbo of database */
  12902.     and (@grantor is null or @grantor = user_name(u.uid))
  12903.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  12904.     and     v.number in (26,193,195,197)
  12905.     and     not exists (        /* exclude revoke'd privileges */
  12906.             select *
  12907.             from    sysprotects p1
  12908.             where   p1.protecttype = 206
  12909.             and     p1.action = v.number
  12910.             and     p1.id = o.id
  12911.             and     p1.uid = o.uid)
  12912.     order by 4, 5, 6, 9, 1, 2
  12913.     END
  12914. ELSE
  12915.     BEGIN
  12916.     select
  12917.         GRANTOR     = user_name(p.grantor),
  12918.         GRANTEE     = user_name(u.uid),
  12919.         TABLE_CATALOG   = db_name(),
  12920.         TABLE_SCHEMA    = user_name(o.uid),
  12921.         TABLE_NAME  = o.name,
  12922.         COLUMN_NAME = c.name,
  12923.         COLUMN_GUID = convert(binary(16),null),
  12924.         COLUMN_PROPID   = convert(int,null),
  12925.         PRIVILEGE_TYPE  = convert(varchar(30),
  12926.                     case p.action
  12927.                     when 193 then 'SELECT'
  12928.                     when 195 then 'INSERT'
  12929.                     when 197 then 'UPDATE'
  12930.                     else 'REFERENCES'
  12931.                     end),
  12932.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  12933.     from 
  12934.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  12935.     where
  12936.         o.type in ('U','V','S')
  12937.     and (@table_schema is null or @table_schema = user_name(o.uid))
  12938.     and c.id = o.id
  12939.     and     (@column_name is null or @column_name = c.name)
  12940.     and     c.id = p.id
  12941.     and (@grantor is null or @grantor = user_name(p.grantor))
  12942.     and     case 
  12943.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12944.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  12945.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  12946.             end & v.high <> 0           /* permission applies to this column */
  12947.     and     v.number <= (select count(*) from syscolumns where id = o.id)   /* ranges from 1 to # of columns in table */
  12948.     and     v.type = 'P'
  12949.     and     v.number = c.colid
  12950.     and (@grantee is null or @grantee = user_name(u.uid))
  12951.             /* expand groups */
  12952.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  12953.     and     p.protecttype <> 206    /* only grant rows */
  12954.     and     p.action in (26,193,195,197)
  12955.     and     o.uid <> u.uid          /* no rows for owner */
  12956.     and     not exists (            /* exclude revoke'd privileges */
  12957.             select *
  12958.             from sysprotects p1
  12959.             where
  12960.                 p1.protecttype = 206
  12961.             and     p1.action = p.action
  12962.             and     p1.id = p.id
  12963.             and     p1.uid = u.uid
  12964.             and     case 
  12965.                 when substring(p1.columns, 1, 1) & 1 is null then 255   /* all columns have permission */
  12966.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  12967.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  12968.                 end & v.high <> 0)          /* permission applies to this column */
  12969.     union
  12970.     select  /*  Add rows for table owner */
  12971.         GRANTOR     = user_name(u.uid),
  12972.         GRANTEE     = user_name(o.uid),
  12973.         TABLE_CATALOG   = db_name(),
  12974.         TABLE_SCHEMA    = user_name(o.uid),
  12975.         TABLE_NAME  = o.name,
  12976.         COLUMN_NAME = c.name,
  12977.         COLUMN_GUID = convert(binary(16),null),
  12978.         COLUMN_PROPID   = convert(int,null),
  12979.         PRIVILEGE_TYPE  = convert(varchar(30),
  12980.                     case v.number
  12981.                     when 193 then 'SELECT'
  12982.                     when 195 then 'INSERT'
  12983.                     when 197 then 'UPDATE'
  12984.                     else 'REFERENCES'
  12985.                     end),
  12986.         IS_GRANTABLE    = convert(bit,1)    
  12987.     from 
  12988.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  12989.     where
  12990.         o.type in ('U','V','S')
  12991.     and (@table_schema is null or @table_schema = user_name(o.uid))
  12992.     and (@grantee is null or @grantee = user_name(o.uid))
  12993.     and c.id = o.id
  12994.     and     (@column_name is null or @column_name = c.name)
  12995.     and     u.uid = 1       /* grantor is dbo of database */
  12996.     and (@grantor is null or @grantor = user_name(u.uid))
  12997.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  12998.     and     v.number in (26,193,195,197)
  12999.     and     not exists (        /* exclude revoke'd privileges */
  13000.             select *
  13001.             from    sysprotects p1
  13002.             where   p1.protecttype = 206
  13003.             and     p1.action = v.number
  13004.             and     p1.id = o.id
  13005.             and     p1.uid = o.uid)
  13006.     order by 4, 5, 6, 9, 1, 2
  13007.     END
  13008. go
  13009. dump tran master with no_log
  13010. go
  13011. CREATE PROCEDURE sp_column_privileges_rowset;2
  13012.     (
  13013.     @handle     int output,
  13014.     @scrollopt  int output,
  13015.     @ccopt      int output,
  13016.     @rows       int output,
  13017.     @table_name     varchar(255) = null,
  13018.     @table_schema   varchar(255) = null,
  13019.     @column_name    varchar(255) = null,
  13020.     @grantor    varchar(255) = null,
  13021.     @grantee    varchar(255) = null
  13022.     )
  13023. as
  13024.  
  13025. declare @ret int
  13026.  
  13027. SET NOCOUNT ON
  13028.     
  13029. create table #spcprivsrowset1
  13030.     (
  13031.     GRANTOR     sysname not null,
  13032.     GRANTEE     sysname not null,
  13033.     TABLE_CATALOG   sysname not null,
  13034.     TABLE_SCHEMA    sysname not null,
  13035.     TABLE_NAME  sysname not null,
  13036.     COLUMN_NAME sysname not null,
  13037.     COLUMN_GUID binary(16) null,
  13038.     COLUMN_PROPID   int null,
  13039.     PRIVILEGE_TYPE  sysname not null,
  13040.     IS_GRANTABLE    bit not null
  13041.     )
  13042.  
  13043. IF @table_name is not null
  13044.     BEGIN
  13045.     insert into #spcprivsrowset1
  13046.     select
  13047.         GRANTOR     = user_name(p.grantor),
  13048.         GRANTEE     = user_name(u.uid),
  13049.         TABLE_CATALOG   = db_name(),
  13050.         TABLE_SCHEMA    = user_name(o.uid),
  13051.         TABLE_NAME  = o.name,
  13052.         COLUMN_NAME = c.name,
  13053.         COLUMN_GUID = convert(binary(16),null),
  13054.         COLUMN_PROPID   = convert(int,null),
  13055.         PRIVILEGE_TYPE  = convert(varchar(30),
  13056.                     case p.action
  13057.                     when 193 then 'SELECT'
  13058.                     when 195 then 'INSERT'
  13059.                     when 197 then 'UPDATE'
  13060.                     else 'REFERENCES'
  13061.                     end),
  13062.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13063.     from 
  13064.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  13065.     where
  13066.         o.name = @table_name
  13067.     and     o.type in ('U','V','S')
  13068.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13069.     and c.id = o.id
  13070.     and     (@column_name is null or @column_name = c.name)
  13071.     and     c.id = p.id
  13072.     and (@grantor is null or @grantor = user_name(p.grantor))
  13073.     and     case 
  13074.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13075.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13076.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13077.             end & v.high <> 0           /* permission applies to this column */
  13078.     and     v.number <= (select count(*) from syscolumns where id = o.id)   /* ranges from 1 to # of columns in table */
  13079.     and     v.type = 'P'
  13080.     and     v.number = c.colid
  13081.     and (@grantee is null or @grantee = user_name(u.uid))
  13082.             /* expand groups */
  13083.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  13084.     and     p.protecttype <> 206    /* only grant rows */
  13085.     and     p.action in (26,193,195,197)
  13086.     and     o.uid <> u.uid          /* no rows for owner */
  13087.     and     not exists (            /* exclude revoke'd privileges */
  13088.             select *
  13089.             from sysprotects p1
  13090.             where
  13091.                 p1.protecttype = 206
  13092.             and     p1.action = p.action
  13093.             and     p1.id = p.id
  13094.             and     p1.uid = u.uid
  13095.             and     case 
  13096.                 when substring(p1.columns, 1, 1) & 1 is null then 255   /* all columns have permission */
  13097.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13098.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13099.                 end & v.high <> 0)          /* permission applies to this column */
  13100.     union
  13101.     select  /*  Add rows for table owner */
  13102.         GRANTOR     = user_name(u.uid),
  13103.         GRANTEE     = user_name(o.uid),
  13104.         TABLE_CATALOG   = db_name(),
  13105.         TABLE_SCHEMA    = user_name(o.uid),
  13106.         TABLE_NAME  = o.name,
  13107.         COLUMN_NAME = c.name,
  13108.         COLUMN_GUID = convert(binary(16),null),
  13109.         COLUMN_PROPID   = convert(int,null),
  13110.         PRIVILEGE_TYPE  = convert(varchar(30),
  13111.                     case v.number
  13112.                     when 193 then 'SELECT'
  13113.                     when 195 then 'INSERT'
  13114.                     when 197 then 'UPDATE'
  13115.                     else 'REFERENCES'
  13116.                     end),
  13117.         IS_GRANTABLE    = convert(bit,1)    
  13118.     from 
  13119.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13120.     where
  13121.         o.name = @table_name
  13122.     and     o.type in ('U','V','S')
  13123.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13124.     and (@grantee is null or @grantee = user_name(o.uid))
  13125.     and c.id = o.id
  13126.     and     (@column_name is null or @column_name = c.name)
  13127.     and     u.uid = 1       /* grantor is dbo of database */
  13128.     and (@grantor is null or @grantor = user_name(u.uid))
  13129.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13130.     and     v.number in (26,193,195,197)
  13131.     and     not exists (        /* exclude revoke'd privileges */
  13132.             select *
  13133.             from    sysprotects p1
  13134.             where   p1.protecttype = 206
  13135.             and     p1.action = v.number
  13136.             and     p1.id = o.id
  13137.             and     p1.uid = o.uid)
  13138.     order by 4, 5, 6, 9, 1, 2
  13139.     END
  13140. ELSE
  13141.     BEGIN
  13142.     insert into #spcprivsrowset1
  13143.     select
  13144.         GRANTOR     = user_name(p.grantor),
  13145.         GRANTEE     = user_name(u.uid),
  13146.         TABLE_CATALOG   = db_name(),
  13147.         TABLE_SCHEMA    = user_name(o.uid),
  13148.         TABLE_NAME  = o.name,
  13149.         COLUMN_NAME = c.name,
  13150.         COLUMN_GUID = convert(binary(16),null),
  13151.         COLUMN_PROPID   = convert(int,null),
  13152.         PRIVILEGE_TYPE  = convert(varchar(30),
  13153.                     case p.action
  13154.                     when 193 then 'SELECT'
  13155.                     when 195 then 'INSERT'
  13156.                     when 197 then 'UPDATE'
  13157.                     else 'REFERENCES'
  13158.                     end),
  13159.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13160.     from 
  13161.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  13162.     where
  13163.         o.type in ('U','V','S')
  13164.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13165.     and c.id = o.id
  13166.     and     (@column_name is null or @column_name = c.name)
  13167.     and     c.id = p.id
  13168.     and (@grantor is null or @grantor = user_name(p.grantor))
  13169.     and     case 
  13170.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13171.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13172.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13173.             end & v.high <> 0           /* permission applies to this column */
  13174.     and     v.number <= (select count(*) from syscolumns where id = o.id)   /* ranges from 1 to # of columns in table */
  13175.     and     v.type = 'P'
  13176.     and     v.number = c.colid
  13177.     and (@grantee is null or @grantee = user_name(u.uid))
  13178.             /* expand groups */
  13179.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  13180.     and     p.protecttype <> 206    /* only grant rows */
  13181.     and     p.action in (26,193,195,197)
  13182.     and     o.uid <> u.uid          /* no rows for owner */
  13183.     and     not exists (            /* exclude revoke'd privileges */
  13184.             select *
  13185.             from sysprotects p1
  13186.             where
  13187.                 p1.protecttype = 206
  13188.             and     p1.action = p.action
  13189.             and     p1.id = p.id
  13190.             and     p1.uid = u.uid
  13191.             and     case 
  13192.                 when substring(p1.columns, 1, 1) & 1 is null then 255   /* all columns have permission */
  13193.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13194.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13195.                 end & v.high <> 0)          /* permission applies to this column */
  13196.     union
  13197.     select  /*  Add rows for table owner */
  13198.         GRANTOR     = user_name(u.uid),
  13199.         GRANTEE     = user_name(o.uid),
  13200.         TABLE_CATALOG   = db_name(),
  13201.         TABLE_SCHEMA    = user_name(o.uid),
  13202.         TABLE_NAME  = o.name,
  13203.         COLUMN_NAME = c.name,
  13204.         COLUMN_GUID = convert(binary(16),null),
  13205.         COLUMN_PROPID   = convert(int,null),
  13206.         PRIVILEGE_TYPE  = convert(varchar(30),
  13207.                     case v.number
  13208.                     when 193 then 'SELECT'
  13209.                     when 195 then 'INSERT'
  13210.                     when 197 then 'UPDATE'
  13211.                     else 'REFERENCES'
  13212.                     end),
  13213.         IS_GRANTABLE    = convert(bit,1)    
  13214.     from 
  13215.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13216.     where
  13217.         o.type in ('U','V','S')
  13218.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13219.     and (@grantee is null or @grantee = user_name(o.uid))
  13220.     and c.id = o.id
  13221.     and     (@column_name is null or @column_name = c.name)
  13222.     and     u.uid = 1       /* grantor is dbo of database */
  13223.     and (@grantor is null or @grantor = user_name(u.uid))
  13224.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13225.     and     v.number in (26,193,195,197)
  13226.     and     not exists (        /* exclude revoke'd privileges */
  13227.             select *
  13228.             from    sysprotects p1
  13229.             where   p1.protecttype = 206
  13230.             and     p1.action = v.number
  13231.             and     p1.id = o.id
  13232.             and     p1.uid = o.uid)
  13233.     order by 4, 5, 6, 9, 1, 2
  13234.     END
  13235.  
  13236. exec @ret = sp_cursoropen @handle output, 'select * from #spcprivsrowset1',
  13237.     @scrollopt output, @ccopt output, @rows output
  13238.  
  13239. drop table #spcprivsrowset1
  13240.  
  13241. return isnull(@ret,0)
  13242. go
  13243. dump tran master with no_log
  13244. go
  13245. CREATE PROCEDURE sp_column_privileges_rowset;3
  13246. as
  13247.     select
  13248.         GRANTOR     = convert(sysname,' '),
  13249.         GRANTEE     = convert(sysname,' '),
  13250.         TABLE_CATALOG   = convert(sysname,' '),
  13251.         TABLE_SCHEMA    = convert(sysname,' '),
  13252.         TABLE_NAME  = convert(sysname,' '),
  13253.         COLUMN_NAME = convert(sysname,' '),
  13254.         COLUMN_GUID = convert(binary(16),null),
  13255.         COLUMN_PROPID   = convert(int,null),
  13256.         PRIVILEGE_TYPE  = convert(varchar(30),' '),
  13257.         IS_GRANTABLE    = convert(bit,1)
  13258.     where   1=0
  13259. go
  13260. dump tran master with no_log
  13261. go
  13262.  
  13263. if (charindex('7.00', @@version) = 0 and
  13264.     charindex('8.00', @@version) = 0)
  13265. begin
  13266.     print ''
  13267.     print ''
  13268.     print 'Warning:'
  13269.     print 'you are installing the stored procedures '
  13270.     print 'on a pre 8.0 SQL Server.'
  13271.     print 'Ignore the following errors.'
  13272. end
  13273. else
  13274.     drop proc sp_column_privileges_rowset
  13275. go
  13276.  
  13277.  
  13278. /*  Procedure for 8.0 server */
  13279. CREATE PROCEDURE sp_column_privileges_rowset
  13280.     (
  13281.     @table_name         sysname,
  13282.     @table_schema       sysname = null,
  13283.     @column_name        sysname = null,
  13284.     @grantor        sysname = null,
  13285.     @grantee        sysname = null
  13286.     )
  13287. as
  13288.     select
  13289.         GRANTOR     = user_name(p.grantor),
  13290.         GRANTEE     = user_name(u.uid),
  13291.         TABLE_CATALOG   = db_name(),
  13292.         TABLE_SCHEMA    = user_name(o.uid),
  13293.         TABLE_NAME  = o.name,
  13294.         COLUMN_NAME = c.name,
  13295.         COLUMN_GUID = convert(uniqueidentifier,null),
  13296.         COLUMN_PROPID   = convert(int,null),
  13297.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  13298.                     case p.action
  13299.                     when 193 then N'SELECT'
  13300.                     when 195 then N'INSERT'
  13301.                     when 197 then N'UPDATE'
  13302.                     else N'REFERENCES'
  13303.                     end),
  13304.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13305.     from 
  13306.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  13307.     where
  13308.         o.name = @table_name
  13309.     and     o.type in ('U','V','S')
  13310.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13311.     and c.id = o.id
  13312.     and     (@column_name is null or @column_name = c.name)
  13313.     and     c.id = p.id
  13314.     and (@grantor is null or @grantor = user_name(p.grantor))
  13315.     and     case 
  13316.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13317.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13318.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13319.             end & v.high <> 0           /* permission applies to this column */
  13320.     and     v.number <= (select count(*) from syscolumns where id = o.id)   /* ranges from 1 to # of columns in table */
  13321.     and     v.type = 'P'
  13322.     and     v.number = c.colid
  13323.     and (@grantee is null or @grantee = user_name(u.uid))
  13324.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  13325.     and     (u.uid > 0 and u.uid < 16384)
  13326.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))  
  13327.     and     p.protecttype <> 206    /* only grant rows */
  13328.     and     p.action in (26,193,195,197)
  13329.     and     o.uid <> u.uid          /* no rows for owner */
  13330.     and     not exists (            /* exclude revoke'd privileges */
  13331.             select *
  13332.             from sysprotects p1
  13333.             where
  13334.                 p1.protecttype = 206
  13335.             and     p1.action = p.action
  13336.             and     p1.id = p.id
  13337.             and     p1.uid = u.uid
  13338.             and     case 
  13339.                 when substring(p1.columns, 1, 1) & 1 is null then 255   /* all columns have permission */
  13340.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13341.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13342.                 end & v.high <> 0)          /* permission applies to this column */
  13343.     union
  13344.     select  /*  Add rows for table owner */
  13345.         GRANTOR     = user_name(u.uid),
  13346.         GRANTEE     = user_name(o.uid),
  13347.         TABLE_CATALOG   = db_name(),
  13348.         TABLE_SCHEMA    = user_name(o.uid),
  13349.         TABLE_NAME  = o.name,
  13350.         COLUMN_NAME = c.name,
  13351.         COLUMN_GUID = convert(uniqueidentifier,null),
  13352.         COLUMN_PROPID   = convert(int,null),
  13353.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  13354.                     case v.number
  13355.                     when 193 then N'SELECT'
  13356.                     when 195 then N'INSERT'
  13357.                     when 197 then N'UPDATE'
  13358.                     else N'REFERENCES'
  13359.                     end),
  13360.         IS_GRANTABLE    = convert(bit,1)    
  13361.     from 
  13362.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13363.     where
  13364.         o.name = @table_name
  13365.     and     o.type in ('U','V','S')
  13366.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13367.     and (@grantee is null or @grantee = user_name(o.uid))
  13368.     and c.id = o.id
  13369.     and     (@column_name is null or @column_name = c.name)
  13370.     and     u.uid = 1       /* grantor is dbo of database */
  13371.     and (@grantor is null or @grantor = user_name(u.uid))
  13372.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13373.     and     v.number in (26,193,195,197)
  13374.     and     not exists (        /* exclude revoke'd privileges */
  13375.             select *
  13376.             from    sysprotects p1
  13377.             where   p1.protecttype = 206
  13378.             and     p1.action = v.number
  13379.             and     p1.id = o.id
  13380.             and     p1.uid = o.uid)
  13381.     order by 4, 5, 6, 9, 1, 2
  13382. go
  13383. dump tran master with no_log
  13384. go
  13385. CREATE PROCEDURE sp_column_privileges_rowset;2
  13386.     (
  13387.     @table_schema       sysname = null,
  13388.     @column_name        sysname = null,
  13389.     @grantor        sysname = null,
  13390.     @grantee        sysname = null
  13391.     )
  13392. as
  13393.     select
  13394.         GRANTOR     = user_name(p.grantor),
  13395.         GRANTEE     = user_name(u.uid),
  13396.         TABLE_CATALOG   = db_name(),
  13397.         TABLE_SCHEMA    = user_name(o.uid),
  13398.         TABLE_NAME  = o.name,
  13399.         COLUMN_NAME = c.name,
  13400.         COLUMN_GUID = convert(uniqueidentifier,null),
  13401.         COLUMN_PROPID   = convert(int,null),
  13402.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  13403.                     case p.action
  13404.                     when 193 then N'SELECT'
  13405.                     when 195 then N'INSERT'
  13406.                     when 197 then N'UPDATE'
  13407.                     else N'REFERENCES'
  13408.                     end),
  13409.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13410.     from 
  13411.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  13412.     where
  13413.         o.type in ('U','V','S')
  13414.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13415.     and c.id = o.id
  13416.     and     (@column_name is null or @column_name = c.name)
  13417.     and     c.id = p.id
  13418.     and (@grantor is null or @grantor = user_name(p.grantor))
  13419.     and     case 
  13420.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13421.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13422.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13423.             end & v.high <> 0           /* permission applies to this column */
  13424.     and     v.number <= (select count(*) from syscolumns where id = o.id)   /* ranges from 1 to # of columns in table */
  13425.     and     v.type = 'P'
  13426.     and     v.number = c.colid
  13427.     and (@grantee is null or @grantee = user_name(u.uid))
  13428.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  13429.     and     (u.uid > 0 and u.uid < 16384)
  13430.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))  
  13431.     and     p.protecttype <> 206    /* only grant rows */
  13432.     and     p.action in (26,193,195,197)
  13433.     and     o.uid <> u.uid          /* no rows for owner */
  13434.     and     not exists (            /* exclude revoke'd privileges */
  13435.             select *
  13436.             from sysprotects p1
  13437.             where
  13438.                 p1.protecttype = 206
  13439.             and     p1.action = p.action
  13440.             and     p1.id = p.id
  13441.             and     p1.uid = u.uid
  13442.             and     case 
  13443.                 when substring(p1.columns, 1, 1) & 1 is null then 255   /* all columns have permission */
  13444.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13445.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13446.                 end & v.high <> 0)          /* permission applies to this column */
  13447.     union
  13448.     select  /*  Add rows for table owner */
  13449.         GRANTOR     = user_name(u.uid),
  13450.         GRANTEE     = user_name(o.uid),
  13451.         TABLE_CATALOG   = db_name(),
  13452.         TABLE_SCHEMA    = user_name(o.uid),
  13453.         TABLE_NAME  = o.name,
  13454.         COLUMN_NAME = c.name,
  13455.         COLUMN_GUID = convert(uniqueidentifier,null),
  13456.         COLUMN_PROPID   = convert(int,null),
  13457.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  13458.                     case v.number
  13459.                     when 193 then N'SELECT'
  13460.                     when 195 then N'INSERT'
  13461.                     when 197 then N'UPDATE'
  13462.                     else N'REFERENCES'
  13463.                     end),
  13464.         IS_GRANTABLE    = convert(bit,1)    
  13465.     from 
  13466.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13467.     where
  13468.         o.type in ('U','V','S')
  13469.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13470.     and (@grantee is null or @grantee = user_name(o.uid))
  13471.     and c.id = o.id
  13472.     and     (@column_name is null or @column_name = c.name)
  13473.     and     u.uid = 1       /* grantor is dbo of database */
  13474.     and (@grantor is null or @grantor = user_name(u.uid))
  13475.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13476.     and     v.number in (26,193,195,197)
  13477.     and     not exists (        /* exclude revoke'd privileges */
  13478.             select *
  13479.             from    sysprotects p1
  13480.             where   p1.protecttype = 206
  13481.             and     p1.action = v.number
  13482.             and     p1.id = o.id
  13483.             and     p1.uid = o.uid)
  13484.     order by 4, 5, 6, 9, 1, 2
  13485. go
  13486. dump tran master with no_log
  13487. go
  13488. create procedure sp_column_privileges_rowset;5
  13489.     (
  13490.     @table_server       sysname,
  13491.     @table_catalog      sysname = null,
  13492.     @table_name     sysname = null,
  13493.     @table_schema       sysname = null,
  13494.     @column_name        sysname = null,
  13495.     @grantor        sysname = null,
  13496.     @grantee        sysname = null
  13497.     )
  13498. as
  13499.     select
  13500.         GRANTOR,
  13501.         GRANTEE,
  13502.         TABLE_CATALOG,
  13503.         TABLE_SCHEMA,
  13504.         TABLE_NAME,
  13505.         COLUMN_NAME,
  13506.         COLUMN_GUID,
  13507.         COLUMN_PROPID,  
  13508.         PRIVILEGE_TYPE,
  13509.         IS_GRANTABLE    
  13510.         from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  13511.                 @table_server,
  13512.                 @table_catalog,
  13513.                 @table_schema,
  13514.                 @table_name,
  13515.                 @column_name,
  13516.                 @grantor,
  13517.                 @grantee>
  13518.     order by 3, 4, 5, 6, 9, 1, 2
  13519. go
  13520.  
  13521. grant execute on sp_column_privileges_rowset to public
  13522. go
  13523.  
  13524. dump tran master with no_log
  13525. go
  13526. if (charindex('6.00', @@version) > 0)
  13527.     begin
  13528.     if (exists (select * from sysobjects
  13529.             where name = 'sp_column_privileges_rowset' and type = 'P '))
  13530.         begin
  13531.         drop procedure sp_column_privileges_rowset
  13532.         dump tran master with no_log
  13533.         end
  13534.     end
  13535. go
  13536.  
  13537.  
  13538. print ''
  13539. print 'creating sp_columns_rowset'
  13540. go
  13541.  
  13542. /*  Procedure for 6.0 and 6.50 server */
  13543. create procedure sp_columns_rowset
  13544.     (
  13545.     @table_name varchar(255),
  13546.     @table_schema   varchar(255) = null,
  13547.     @column_name    varchar(255) = null
  13548.     )
  13549. as  
  13550.     select
  13551.         TABLE_CATALOG       = db_name(),
  13552.         TABLE_SCHEMA        = user_name(o.uid),
  13553.         TABLE_NAME      = o.name,
  13554.         COLUMN_NAME     = c.name,
  13555.         COLUMN_GUID     = convert(binary(16),null),
  13556.         COLUMN_PROPID       = convert(int,null),
  13557.         ORDINAL_POSITION    = convert(int,
  13558.                        (
  13559.                         select count(*)
  13560.                         from syscolumns sc
  13561.                         where sc.id     =  c.id
  13562.                           AND sc.number =  c.number
  13563.                           AND sc.colid  <= c.colid
  13564.                         )),
  13565.         COLUMN_HASDEFAULT   = convert(bit, case when m.text is null then 0 else 1 end),
  13566.  
  13567.         COLUMN_DEFAULT      = convert (varchar (255),
  13568.                                   case when substring(m.text,1,1) = '('
  13569.                                   then substring(m.text,2,datalength(m.text)-2)
  13570.                                   else substring(m.text,1,datalength(m.text)-2)
  13571.                               end), 
  13572.         COLUMN_FLAGS        = convert(int,
  13573.                         case when d.is_long = 1 
  13574.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13575.                         else 0
  13576.                         end
  13577.                     |   case when d.fixlen is not null
  13578.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13579.                         else 0
  13580.                         end
  13581.                     |   case when c.status&8 = 8
  13582.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13583.                         else 0
  13584.                         end
  13585.                     |   case 
  13586.                         when d.type_name = 'timestamp'
  13587.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13588.                         when (c.status&128) != 128
  13589.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  13590.                         else 0 
  13591.                         end),
  13592.         IS_NULLABLE     = convert(bit,c.status&8),
  13593.         DATA_TYPE       = d.oledb_data_type,
  13594.         TYPE_GUID       = convert(binary(16),null),
  13595.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13596.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13597.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13598.                         then coalesce(d.column_size,c.length)
  13599.                         else null 
  13600.                         end),
  13601.         CHARACTER_OCTET_LENGTH  = convert(int,
  13602.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13603.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13604.                         then coalesce(d.column_size,c.length)
  13605.                         else null 
  13606.                         end),
  13607.         NUMERIC_PRECISION   = convert(smallint,
  13608.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  13609.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13610.                                     then d.data_precision else null end),
  13611.         NUMERIC_SCALE       = convert(smallint, 
  13612.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13613.         DATETIME_PRECISION  = convert(int,
  13614.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13615.                             when data_precision = 23 then 3 else 0 end),
  13616.         CHARACTER_SET_CATALOG   = convert(sysname,
  13617.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13618.                         then 'master' 
  13619.                         else null 
  13620.                         end),
  13621.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13622.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13623.                         then 'dbo' 
  13624.                         else null 
  13625.                         end),
  13626.         CHARACTER_SET_NAME  = convert(sysname,
  13627.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13628.                         then a_cha.name 
  13629.                         else null 
  13630.                         end),
  13631.         COLLATION_CATALOG   = convert(sysname,
  13632.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13633.                         then 'master' 
  13634.                         else null 
  13635.                         end),
  13636.         COLLATION_SCHEMA    = convert(sysname,
  13637.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13638.                         then 'dbo' 
  13639.                         else null 
  13640.                         end),
  13641.         COLLATION_NAME      = convert(sysname,
  13642.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13643.                         then b_cha.name 
  13644.                         else null 
  13645.                         end),
  13646.         DOMAIN_CATALOG      = case when t.usertype < 256 then null else db_name() end,
  13647.         DOMAIN_SCHEMA       = case when t.usertype < 256 then null else user_name(o.uid) end,
  13648.         DOMAIN_NAME     = case when t.usertype < 256 then null else t.name end,
  13649.         DESCRIPTION     = convert(varchar(1),null)  
  13650.     
  13651.     from
  13652.         syscolumns c,
  13653.         syscomments m,
  13654.         sysobjects o,
  13655.         master.dbo.spt_provider_types d,
  13656.         systypes t,
  13657.         sysusers u,
  13658.         master.dbo.sysconfigures    cfg,
  13659.         master.dbo.syscharsets      a_cha, /* charset/1001, not sortorder. */
  13660.         master.dbo.syscharsets      b_cha  /* sortorder/2001, not charset. */
  13661.     where
  13662.         o.name = @table_name
  13663.     and     o.type in ('U','V','S')
  13664.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  13665.     and     (
  13666.         suser_id() = 1   /* User is the System Administrator */
  13667.         or o.uid = user_id()     /* User created the object */
  13668.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  13669.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  13670.             from sysprotects p
  13671.             where p.id = o.id
  13672.             /* get rows for public,current user,user's group */
  13673.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  13674.             /* check for SELECT,EXECUTE privilege */
  13675.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  13676.             ) = 1   /* final magic...compare Grants   */
  13677.            )
  13678.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13679.     and (@column_name is null or @column_name = c.name)
  13680.     and     o.id = c.id
  13681.     and     t.type = d.ss_dtype
  13682.     and (t.usertype != 80 or d.type_name='timestamp')
  13683.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13684.     and     c.usertype = t.usertype
  13685.     and c.cdefault *= m.id
  13686.     and m.colid = 1
  13687.     and cfg.comment = 'default sortorder ID' 
  13688.     and a_cha.type = 1001 /* type is charset */
  13689.     and b_cha.type = 2001 /* type is sortorder */
  13690.     and a_cha.id = b_cha.csid
  13691.     and     b_cha.id = cfg.value
  13692.     order by 2, 3, c.colid
  13693. go
  13694.  
  13695. dump tran master with no_log
  13696. go
  13697.  
  13698. create procedure sp_columns_rowset;2
  13699.     (
  13700.     @table_schema   varchar(255) = null,
  13701.     @column_name    varchar(255) = null
  13702.     )
  13703. as  
  13704.     select
  13705.         TABLE_CATALOG       = db_name(),
  13706.         TABLE_SCHEMA        = user_name(o.uid),
  13707.         TABLE_NAME      = o.name,
  13708.         COLUMN_NAME     = c.name,
  13709.         COLUMN_GUID     = convert(binary(16),null),
  13710.         COLUMN_PROPID       = convert(int,null),
  13711.         ORDINAL_POSITION    = convert(int,
  13712.                        (
  13713.                         select count(*)
  13714.                         from syscolumns sc
  13715.                         where sc.id     =  c.id
  13716.                           AND sc.number =  c.number
  13717.                           AND sc.colid  <= c.colid
  13718.                         )),
  13719.         COLUMN_HASDEFAULT   = convert(bit, case when m.text is null then 0 else 1 end),
  13720.         COLUMN_DEFAULT      = convert (varchar (255),
  13721.                                   case when substring(m.text,1,1) = '('
  13722.                                   then substring(m.text,2,datalength(m.text)-2)
  13723.                                   else substring(m.text,1,datalength(m.text)-2)
  13724.                               end), 
  13725.         COLUMN_FLAGS        = convert(int,
  13726.                         case when d.is_long = 1 
  13727.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13728.                         else 0
  13729.                         end
  13730.                     |   case when d.fixlen is not null
  13731.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13732.                         else 0
  13733.                         end
  13734.                     |   case when c.status&8 = 8
  13735.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13736.                         else 0
  13737.                         end
  13738.                     |   case 
  13739.                         when d.type_name = 'timestamp'
  13740.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13741.                         when (c.status&128) != 128
  13742.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  13743.                         else 0 
  13744.                         end),
  13745.         IS_NULLABLE     = convert(bit,c.status&8),
  13746.         DATA_TYPE       = d.oledb_data_type,
  13747.         TYPE_GUID       = convert(binary(16),null),
  13748.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13749.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13750.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13751.                         then coalesce(d.column_size,c.length)
  13752.                         else null 
  13753.                         end),
  13754.         CHARACTER_OCTET_LENGTH  = convert(int,
  13755.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13756.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13757.                         then coalesce(d.column_size,c.length)
  13758.                         else null 
  13759.                         end),
  13760.         NUMERIC_PRECISION   = convert(smallint,
  13761.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  13762.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13763.                             then d.data_precision else null end),
  13764.         NUMERIC_SCALE       = convert(smallint, 
  13765.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13766.         DATETIME_PRECISION  = convert(int,
  13767.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13768.                             when data_precision = 23 then 3 else 0 end),
  13769.         CHARACTER_SET_CATALOG   = convert(sysname,
  13770.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13771.                         then 'master' 
  13772.                         else null 
  13773.                         end),
  13774.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13775.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13776.                         then 'dbo' 
  13777.                         else null 
  13778.                         end),
  13779.         CHARACTER_SET_NAME  = convert(sysname,
  13780.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13781.                         then a_cha.name 
  13782.                         else null 
  13783.                         end),
  13784.         COLLATION_CATALOG   = convert(sysname,
  13785.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13786.                         then 'master' 
  13787.                         else null 
  13788.                         end),
  13789.         COLLATION_SCHEMA    = convert(sysname,
  13790.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13791.                         then 'dbo' 
  13792.                         else null 
  13793.                         end),
  13794.         COLLATION_NAME      = convert(sysname,
  13795.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13796.                         then b_cha.name 
  13797.                         else null 
  13798.                         end),
  13799.         DOMAIN_CATALOG      = case when t.usertype < 256 then null else db_name() end,
  13800.         DOMAIN_SCHEMA       = case when t.usertype < 256 then null else user_name(o.uid) end,
  13801.         DOMAIN_NAME     = case when t.usertype < 256 then null else t.name end,
  13802.         DESCRIPTION     = convert(varchar(1),null)  
  13803.     
  13804.     from
  13805.         syscolumns c,
  13806.         syscomments m,
  13807.         sysobjects o,
  13808.         master.dbo.spt_provider_types d,
  13809.         systypes t,
  13810.         sysusers u,
  13811.         master.dbo.sysconfigures    cfg,
  13812.         master.dbo.syscharsets      a_cha, /* charset/1001, not sortorder. */
  13813.         master.dbo.syscharsets      b_cha /* sortorder/2001, not charset. */
  13814.     where
  13815.         o.type in ('U','V','S')
  13816.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  13817.     and     (
  13818.         suser_id() = 1   /* User is the System Administrator */
  13819.         or o.uid = user_id()     /* User created the object */
  13820.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  13821.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  13822.             from sysprotects p
  13823.             where p.id = o.id
  13824.             /* get rows for public,current user,user's group */
  13825.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  13826.             /* check for SELECT,EXECUTE privilege */
  13827.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  13828.             ) = 1   /* final magic...compare Grants   */
  13829.            )
  13830.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13831.     and (@column_name is null or @column_name = c.name)
  13832.     and     o.id = c.id
  13833.     and     t.type = d.ss_dtype
  13834.     and (t.usertype != 80 or d.type_name='timestamp')
  13835.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13836.     and     c.usertype = t.usertype
  13837.     and c.cdefault *= m.id
  13838.     and  m.colid = 1
  13839.     and cfg.comment = 'default sortorder ID' 
  13840.     and a_cha.type = 1001 /* type is charset */
  13841.     and b_cha.type = 2001 /* type is sortorder */
  13842.     and a_cha.id = b_cha.csid
  13843.     and     b_cha.id = cfg.value
  13844.     order by 2, 3, c.colid
  13845.     
  13846. go
  13847.  
  13848. dump tran master with no_log
  13849. go
  13850.  
  13851. if (charindex('7.00', @@version) = 0 and
  13852.     charindex('8.00', @@version) = 0)
  13853. begin
  13854.     print ''
  13855.     print ''
  13856.     print 'Warning:'
  13857.     print 'you are installing the stored procedures '
  13858.     print 'on a pre 8.0 SQL Server.'
  13859.     print 'Ignore the following errors.'
  13860. end
  13861. else
  13862.     drop proc sp_columns_rowset
  13863. go
  13864.  
  13865. /*  Procedure for 7.0 server */
  13866. create procedure sp_columns_rowset
  13867.     (
  13868.     @table_name sysname,
  13869.     @table_schema   sysname = NULL,
  13870.     @column_name    sysname = NULL
  13871.     )
  13872. as
  13873.     select
  13874.         TABLE_CATALOG       = db_name(),
  13875.         TABLE_SCHEMA        = user_name(o.uid),
  13876.         TABLE_NAME      = o.name,
  13877.         COLUMN_NAME     = c.name,
  13878.         COLUMN_GUID     = convert(uniqueidentifier,null),
  13879.         COLUMN_PROPID       = convert(int,null),
  13880.         ORDINAL_POSITION    = convert(int,
  13881.                        (
  13882.                         select count(*)
  13883.                         from syscolumns sc
  13884.                         where sc.id     =  c.id
  13885.                           AND sc.number =  c.number
  13886.                           AND sc.colid  <= c.colid
  13887.                         )),
  13888.         COLUMN_HASDEFAULT   = convert(bit, case when m.text is null then 0 else 1 end),
  13889.         COLUMN_DEFAULT      =  convert(nvarchar(2000),
  13890.                                         case when substring(m.text,1,1) = '('
  13891.                                             then substring(m.text,2,datalength(m.text)/2-2)
  13892.                                             else substring(m.text,1,datalength(m.text)/2-2)
  13893.                                         end ), 
  13894.         COLUMN_FLAGS        = convert(int,
  13895.                         case when d.is_long = 1 
  13896.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13897.                         else 0
  13898.                         end
  13899.                     |   case when d.fixlen is not null
  13900.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13901.                         else 0
  13902.                         end
  13903.                     |   case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  13904.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13905.                         else 0
  13906.                         end
  13907.                     |   case 
  13908.                         when d.type_name = 'timestamp'
  13909.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13910.                         when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
  13911.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  13912.                         else 0 
  13913.                         end),
  13914.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  13915.         DATA_TYPE       = d.oledb_data_type,
  13916.         TYPE_GUID       = convert(uniqueidentifier,null),
  13917.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13918.                         case 
  13919.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13920.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13921.                         then coalesce(d.column_size,c.length)
  13922.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  13923.                         then coalesce(d.column_size,c.length/2)
  13924.                         else null 
  13925.                         end),
  13926.         CHARACTER_OCTET_LENGTH  = convert(int,
  13927.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13928.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13929.                         then coalesce(d.column_size,c.length)
  13930.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  13931.                         then coalesce(d.column_size*2,c.length)
  13932.                         else null 
  13933.                         end),
  13934.         NUMERIC_PRECISION   = convert(smallint,
  13935.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  13936.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13937.                             then d.data_precision else null end),
  13938.         NUMERIC_SCALE       = convert(smallint, 
  13939.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13940.         DATETIME_PRECISION  = convert(int,
  13941.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13942.                             when data_precision = 23 then 3 else 0 end),
  13943.         CHARACTER_SET_CATALOG   = convert(sysname,
  13944.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13945.                         then N'master' 
  13946.                         else null 
  13947.                         end),
  13948.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13949.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13950.                         then N'dbo' 
  13951.                         else null 
  13952.                         end),
  13953.         CHARACTER_SET_NAME  = convert(sysname,
  13954.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13955.                         then a_cha.name 
  13956.                         else null 
  13957.                         end),
  13958.         COLLATION_CATALOG   = convert(sysname,
  13959.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13960.                         then N'master' 
  13961.                         else null 
  13962.                         end),
  13963.         COLLATION_SCHEMA    = convert(sysname,
  13964.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13965.                         then N'dbo' 
  13966.                         else null 
  13967.                         end),
  13968.         COLLATION_NAME      = convert(sysname,
  13969.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13970.                         then b_cha.name 
  13971.                         else null 
  13972.                         end),
  13973.         DOMAIN_CATALOG      = case when t.usertype < 256 then null else db_name() end,
  13974.         DOMAIN_SCHEMA       = case when t.usertype < 256 then null else user_name(o.uid) end,
  13975.         DOMAIN_NAME     = case when t.usertype < 256 then null else t.name end,
  13976.         DESCRIPTION     = convert(nvarchar(1),null) 
  13977.  
  13978.     from
  13979.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  13980.         sysobjects o,
  13981.         master.dbo.spt_provider_types d,
  13982.         systypes t,
  13983.         master.dbo.sysconfigures    cfg,
  13984.         master.dbo.syscharsets      a_cha, /* charset/1001, not sortorder. */
  13985.         master.dbo.syscharsets      b_cha /* sortorder/2001, not charset. */
  13986.     where
  13987.         permissions(o.id, c.name) <> 0
  13988.     and o.name = @table_name
  13989.     and     o.type in ('U','V','S')
  13990.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13991.     and (@column_name is null or @column_name = c.name)
  13992.     and     o.id = c.id
  13993.     and     t.xtype = d.ss_dtype
  13994.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13995.     and     c.xusertype = t.xusertype
  13996.     and cfg.comment = 'default sortorder id' 
  13997.     and a_cha.type = 1001 /* type is charset */
  13998.     and b_cha.type = 2001 /* type is sortorder */
  13999.     and a_cha.id = b_cha.csid
  14000.     and     b_cha.id = cfg.value
  14001.     order by 2, 3, c.colorder
  14002. go
  14003.  
  14004. dump tran master with no_log
  14005. go
  14006.  
  14007. /*  Procedure for 7.0 server */
  14008. create procedure sp_columns_rowset;2
  14009.     (
  14010.     @table_schema   sysname = NULL,
  14011.     @column_name    sysname = NULL
  14012.     )
  14013. as  
  14014.     select
  14015.         TABLE_CATALOG       = db_name(),
  14016.         TABLE_SCHEMA        = user_name(o.uid),
  14017.         TABLE_NAME      = o.name,
  14018.         COLUMN_NAME     = c.name,
  14019.         COLUMN_GUID     = convert(uniqueidentifier,null),
  14020.         COLUMN_PROPID       = convert(int,null),
  14021.         ORDINAL_POSITION    = convert(int,
  14022.                        (
  14023.                         select count(*)
  14024.                         from syscolumns sc
  14025.                         where sc.id     =  c.id
  14026.                           AND sc.number =  c.number
  14027.                           AND sc.colid  <= c.colid
  14028.                         )),
  14029.         COLUMN_HASDEFAULT   = convert(bit, case when m.text is null then 0 else 1 end),
  14030.         COLUMN_DEFAULT      =  convert(nvarchar(2000),
  14031.                                         case when substring(m.text,1,1) = '('
  14032.                                             then substring(m.text,2,datalength(m.text)/2-2)
  14033.                                             else substring(m.text,1,datalength(m.text)/2-2)
  14034.                                         end ), 
  14035.         COLUMN_FLAGS        = convert(int,
  14036.                         case when d.is_long = 1 
  14037.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  14038.                         else 0
  14039.                         end
  14040.                     |   case when d.fixlen is not null
  14041.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  14042.                         else 0
  14043.                         end
  14044.                     |   case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  14045.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  14046.                         else 0
  14047.                         end
  14048.                     |   case 
  14049.                         when d.type_name = 'timestamp'
  14050.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  14051.                         when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
  14052.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  14053.                         else 0 
  14054.                         end),
  14055.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  14056.         DATA_TYPE       = d.oledb_data_type,
  14057.         TYPE_GUID       = convert(uniqueidentifier,null),
  14058.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  14059.                         case 
  14060.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14061.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14062.                         then coalesce(d.column_size,c.length)
  14063.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14064.                         then coalesce(d.column_size,c.length/2)
  14065.                         else null 
  14066.                         end),
  14067.         CHARACTER_OCTET_LENGTH  = convert(int,
  14068.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14069.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14070.                         then coalesce(d.column_size,c.length)
  14071.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14072.                         then coalesce(d.column_size*2,c.length)
  14073.                         else null 
  14074.                         end),
  14075.         NUMERIC_PRECISION   = convert(smallint,
  14076.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  14077.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  14078.                             then d.data_precision else null end),
  14079.         NUMERIC_SCALE       = convert(smallint, 
  14080.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  14081.         DATETIME_PRECISION  = convert(int,
  14082.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  14083.                             when data_precision = 23 then 3 else 0 end),
  14084.         CHARACTER_SET_CATALOG   = convert(sysname,
  14085.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14086.                         then N'master' 
  14087.                         else null 
  14088.                         end),
  14089.         CHARACTER_SET_SCHEMA    = convert(sysname,
  14090.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14091.                         then N'dbo' 
  14092.                         else null 
  14093.                         end),
  14094.         CHARACTER_SET_NAME  = convert(sysname,
  14095.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14096.                         then a_cha.name 
  14097.                         else null 
  14098.                         end),
  14099.         COLLATION_CATALOG   = convert(sysname,
  14100.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14101.                         then N'master' 
  14102.                         else null 
  14103.                         end),
  14104.         COLLATION_SCHEMA    = convert(sysname,
  14105.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14106.                         then N'dbo' 
  14107.                         else null 
  14108.                         end),
  14109.         COLLATION_NAME      = convert(sysname,
  14110.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14111.                         then b_cha.name 
  14112.                         else null 
  14113.                         end),
  14114.         DOMAIN_CATALOG      = case when t.usertype < 256 then null else db_name() end,
  14115.         DOMAIN_SCHEMA       = case when t.usertype < 256 then null else user_name(o.uid) end,
  14116.         DOMAIN_NAME     = case when t.usertype < 256 then null else t.name end,
  14117.         DESCRIPTION     = convert(nvarchar(1),null) 
  14118.     
  14119.     from
  14120.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  14121.         sysobjects o,
  14122.         master.dbo.spt_provider_types d,
  14123.         systypes t,
  14124.         master.dbo.sysconfigures    cfg,
  14125.         master.dbo.syscharsets      a_cha, /* charset/1001, not sortorder.*/
  14126.         master.dbo.syscharsets      b_cha /* sortorder/2001, not charset.*/
  14127.     where
  14128.         permissions(o.id, c.name) <> 0
  14129.     and     o.type in ('U','V','S')
  14130.     and (@table_schema is null or @table_schema = user_name(o.uid))
  14131.     and (@column_name is null or @column_name = c.name)
  14132.     and     o.id = c.id
  14133.     and     t.xtype = d.ss_dtype
  14134.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  14135.     and     c.xusertype = t.xusertype
  14136.     and cfg.comment = 'default sortorder id' 
  14137.     and     a_cha.type = 1001 /* type is charset */
  14138.     and     b_cha.type = 2001 /* type is sortorder */
  14139.     and a_cha.id = b_cha.csid
  14140.     and     b_cha.id = cfg.value
  14141.     order by 2, 3, c.colorder
  14142. go
  14143.  
  14144. dump tran master with no_log
  14145. go
  14146.  
  14147. /*  Procedure for 7.0 server */
  14148. create procedure sp_columns_rowset;5
  14149.     (
  14150.     @table_server       sysname,
  14151.     @table_catalog      sysname = null,
  14152.     @table_name     sysname = null,
  14153.     @table_schema       sysname = null,
  14154.     @column_name        sysname = null
  14155.     )
  14156. as
  14157.     select
  14158.         TABLE_CATALOG,
  14159.         TABLE_SCHEMA,
  14160.         TABLE_NAME,
  14161.         COLUMN_NAME,
  14162.         COLUMN_GUID,
  14163.         COLUMN_PROPID,
  14164.         ORDINAL_POSITION,
  14165.         COLUMN_HASDEFAULT,
  14166.         COLUMN_DEFAULT,
  14167.         COLUMN_FLAGS,
  14168.         IS_NULLABLE,
  14169.         DATA_TYPE,
  14170.         TYPE_GUID,
  14171.         CHARACTER_MAXIMUM_LENGTH,
  14172.         CHARACTER_OCTET_LENGTH,
  14173.         NUMERIC_PRECISION,
  14174.         NUMERIC_SCALE,
  14175.         DATETIME_PRECISION,
  14176.         CHARACTER_SET_CATALOG,
  14177.         CHARACTER_SET_SCHEMA,
  14178.         CHARACTER_SET_NAME,
  14179.         COLLATION_CATALOG,
  14180.         COLLATION_SCHEMA,
  14181.         COLLATION_NAME,
  14182.         DOMAIN_CATALOG,
  14183.         DOMAIN_SCHEMA,
  14184.         DOMAIN_NAME,
  14185.         DESCRIPTION
  14186.     from master.dbo.SYSREMOTE_COLUMNS <
  14187.                 @table_server,
  14188.                 @table_catalog,
  14189.                 @table_schema,
  14190.                 @table_name,
  14191.                 @column_name > 
  14192.     order by 1, 2, 3, 7
  14193. go
  14194.  
  14195.  
  14196. dump tran master with no_log
  14197. go
  14198.  
  14199. /*  Procedure for 8.00 server */
  14200. if (charindex('8.00', @@version) = 0)
  14201. begin
  14202.     print ''
  14203.     print ''
  14204.     print 'Warning:'
  14205.     print 'you are installing the stored procedures '
  14206.     print 'on a pre 8.0 SQL Server.'
  14207.     print 'Ignore the following errors.'
  14208. end
  14209. else
  14210.     drop proc sp_columns_rowset
  14211. go
  14212.  
  14213. /*  Procedure for 8.00 server */
  14214. create procedure sp_columns_rowset
  14215.     (
  14216.     @table_name sysname,
  14217.     @table_schema   sysname = NULL,
  14218.     @column_name    sysname = NULL
  14219.     )
  14220. as  
  14221.     select
  14222.         TABLE_CATALOG       = db_name(),
  14223.         TABLE_SCHEMA        = user_name(o.uid),
  14224.         TABLE_NAME      = o.name,
  14225.         COLUMN_NAME     = c.name,
  14226.         COLUMN_GUID     = convert(uniqueidentifier,null),
  14227.         COLUMN_PROPID       = convert(int,null),
  14228.         ORDINAL_POSITION    = convert(int,
  14229.                        (
  14230.                         select count(*)
  14231.                         from syscolumns sc
  14232.                         where sc.id     =  c.id
  14233.                           AND sc.number =  c.number
  14234.                           AND sc.colid  <= c.colid
  14235.                         )),
  14236.         COLUMN_HASDEFAULT   = convert(bit, case when m.text is null then 0 else 1 end),
  14237.  
  14238.         /* Get rid of ( if it is first char otherwise display the whole thing */
  14239.         COLUMN_DEFAULT      = convert(nvarchar(2000),
  14240.                                         case when substring(m.text,1,1) = '('
  14241.                                             then substring(m.text,2,datalength(m.text)/2-2)
  14242.                                             else substring(m.text,1,datalength(m.text)/2-2)
  14243.                                         end ), 
  14244.  
  14245.         COLUMN_FLAGS        = convert(int,
  14246.                         case when d.is_long = 1 
  14247.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  14248.                         else 0
  14249.                         end
  14250.                     |   case when d.fixlen is not null
  14251.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  14252.                         else 0
  14253.                         end
  14254.                     |   case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  14255.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  14256.                         else 0
  14257.                         end
  14258.                     |   case 
  14259.                         when d.type_name = 'timestamp'
  14260.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  14261.                         when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
  14262.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  14263.                         else 0 
  14264.                         end),
  14265.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  14266.         DATA_TYPE       = d.oledb_data_type,
  14267.         TYPE_GUID       = convert(uniqueidentifier,null),
  14268.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  14269.                         case 
  14270.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14271.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14272.                         then coalesce(d.column_size,c.length)
  14273.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14274.                         then coalesce(d.column_size,c.length/2)
  14275.                         else null 
  14276.                         end),
  14277.         CHARACTER_OCTET_LENGTH  = convert(int,
  14278.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14279.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14280.                         then coalesce(d.column_size,c.length)
  14281.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14282.                         then coalesce(d.column_size*2,c.length)
  14283.                         else null 
  14284.                         end),
  14285.         NUMERIC_PRECISION   = convert(smallint,
  14286.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  14287.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  14288.                             then d.data_precision else null end),
  14289.         NUMERIC_SCALE       = convert(smallint, 
  14290.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  14291.         DATETIME_PRECISION  = convert(int,
  14292.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  14293.                             when data_precision = 23 then 3 else 0 end),
  14294.         CHARACTER_SET_CATALOG   = convert(sysname,
  14295.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14296.                         then N'master' 
  14297.                         else null 
  14298.                         end),
  14299.         CHARACTER_SET_SCHEMA    = convert(sysname,
  14300.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14301.                         then N'dbo' 
  14302.                         else null 
  14303.                         end),
  14304.         CHARACTER_SET_NAME  = convert(sysname,
  14305.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14306.                         then a_cha.name 
  14307.                         else null 
  14308.                         end),
  14309.         COLLATION_CATALOG   = convert(sysname,
  14310.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14311.                         then N'master' 
  14312.                         else null 
  14313.                         end),
  14314.         COLLATION_SCHEMA    = convert(sysname,
  14315.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14316.                         then N'dbo' 
  14317.                         else null 
  14318.                         end),
  14319.         COLLATION_NAME      = convert(sysname,
  14320.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14321.                         then c.collation 
  14322.                         else null 
  14323.                         end),
  14324.         DOMAIN_CATALOG      = case when t.usertype < 256 then null else db_name() end,
  14325.         DOMAIN_SCHEMA       = case when t.usertype < 256 then null else user_name(o.uid) end,
  14326.         DOMAIN_NAME     = case when t.usertype < 256 then null else t.name end,
  14327.         DESCRIPTION     = convert(nvarchar(1),null),
  14328.         COLUMN_LCID     = convert(int, CollationPropertyFromID(c.collationid, 'lcid')),
  14329.         COLUMN_COMPFLAGS = convert(int, CollationPropertyFromID(c.collationid, 'oledbcompstyle')),
  14330.         COLUMN_SORTID = case /* hack to keep the old behavior: will be removed */
  14331.                             when ABS(c.collationid) > 0x1000000 
  14332.                             then convert(int, DATABASEPROPERTYEX(db_name(),'sqlsortorder'))
  14333.                             else null 
  14334.                         end,
  14335.         COLUMN_TDSCOLLATION = convert (binary(5),CollationPropertyFromID(c.collationid, 'TDSCollation')),
  14336.         IS_COMPUTED = convert(bit, c.iscomputed)    
  14337.     from
  14338.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  14339.         sysobjects o,
  14340.         master.dbo.spt_provider_types d,
  14341.         systypes t,
  14342.         master.dbo.syscharsets      a_cha /* charset/1001, not sortorder. */
  14343.     where
  14344.         permissions(o.id, c.name) <> 0
  14345.     and o.name = @table_name
  14346.     and     (o.type in ('U','V','S') OR (o.type in ('TF', 'IF') and c.number = 0))
  14347.     and (@table_schema is null or @table_schema = user_name(o.uid))
  14348.     and (@column_name is null or @column_name = c.name)
  14349.     and     o.id = c.id
  14350.     and     t.xtype = d.ss_dtype
  14351.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  14352.     and     c.xusertype = t.xusertype
  14353.     and a_cha.id = isnull(convert(tinyint, CollationPropertyFromID(c.collationid, 'sqlcharset')),
  14354.             convert(tinyint, ServerProperty('sqlcharset'))) -- make sure there's one and only one row selected for each column
  14355.     order by 2, 3, c.colorder
  14356. go
  14357.  
  14358.  
  14359. /*  Procedure for 8.0 server */
  14360. create procedure sp_columns_rowset;2
  14361.     (
  14362.     @table_schema   sysname = NULL,
  14363.     @column_name    sysname = NULL
  14364.     )
  14365. as  
  14366.     select
  14367.         TABLE_CATALOG       = db_name(),
  14368.         TABLE_SCHEMA        = user_name(o.uid),
  14369.         TABLE_NAME      = o.name,
  14370.         COLUMN_NAME     = c.name,
  14371.         COLUMN_GUID     = convert(uniqueidentifier,null),
  14372.         COLUMN_PROPID       = convert(int,null),
  14373.         ORDINAL_POSITION    = convert(int,
  14374.                        (
  14375.                         select count(*)
  14376.                         from syscolumns sc
  14377.                         where sc.id     =  c.id
  14378.                           AND sc.number =  c.number
  14379.                           AND sc.colid  <= c.colid
  14380.                         )),
  14381.         COLUMN_HASDEFAULT   = convert(bit, case when m.text is null then 0 else 1 end),
  14382.         COLUMN_DEFAULT      = convert(nvarchar(2000),
  14383.                                         case when substring(m.text,1,1) = '('
  14384.                                             then substring(m.text,2,datalength(m.text)/2-2)
  14385.                                             else substring(m.text,1,datalength(m.text)/2-2)
  14386.                                         end ), 
  14387.         COLUMN_FLAGS        = convert(int,
  14388.                         case when d.is_long = 1 
  14389.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  14390.                         else 0
  14391.                         end
  14392.                     |   case when d.fixlen is not null
  14393.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  14394.                         else 0
  14395.                         end
  14396.                     |   case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  14397.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  14398.                         else 0
  14399.                         end
  14400.                     |   case 
  14401.                         when d.type_name = 'timestamp'
  14402.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  14403.                         when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
  14404.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  14405.                         else 0 
  14406.                         end),
  14407.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  14408.         DATA_TYPE       = d.oledb_data_type,
  14409.         TYPE_GUID       = convert(uniqueidentifier,null),
  14410.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  14411.                         case 
  14412.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14413.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14414.                         then coalesce(d.column_size,c.length)
  14415.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14416.                         then coalesce(d.column_size,c.length/2)
  14417.                         else null 
  14418.                         end),
  14419.         CHARACTER_OCTET_LENGTH  = convert(int,
  14420.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14421.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14422.                         then coalesce(d.column_size,c.length)
  14423.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14424.                         then coalesce(d.column_size*2,c.length)
  14425.                         else null 
  14426.                         end),
  14427.         NUMERIC_PRECISION   = convert(smallint,
  14428.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  14429.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  14430.                             then d.data_precision else null end),
  14431.         NUMERIC_SCALE       = convert(smallint, 
  14432.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  14433.         DATETIME_PRECISION  = convert(int,
  14434.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  14435.                             when data_precision = 23 then 3 else 0 end),
  14436.         CHARACTER_SET_CATALOG   = convert(sysname,
  14437.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14438.                         then N'master' 
  14439.                         else null 
  14440.                         end),
  14441.         CHARACTER_SET_SCHEMA    = convert(sysname,
  14442.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14443.                         then N'dbo' 
  14444.                         else null 
  14445.                         end),
  14446.         CHARACTER_SET_NAME  = convert(sysname,
  14447.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14448.                         then a_cha.name 
  14449.                         else null 
  14450.                         end),
  14451.         COLLATION_CATALOG   = convert(sysname,
  14452.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14453.                         then N'master' 
  14454.                         else null 
  14455.                         end),
  14456.         COLLATION_SCHEMA    = convert(sysname,
  14457.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14458.                         then N'dbo' 
  14459.                         else null 
  14460.                         end),
  14461.         COLLATION_NAME      = convert(sysname,
  14462.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14463.                         then c.collation 
  14464.                         else null 
  14465.                         end),
  14466.         DOMAIN_CATALOG      = case when t.usertype < 256 then null else db_name() end,
  14467.         DOMAIN_SCHEMA       = case when t.usertype < 256 then null else user_name(o.uid) end,
  14468.         DOMAIN_NAME     = case when t.usertype < 256 then null else t.name end,
  14469.         DESCRIPTION     = convert(nvarchar(1),null),    
  14470.         COLUMN_LCID     = convert(int, CollationPropertyFromID(c.collationid, 'lcid')),
  14471.         COLUMN_COMPFLAGS = convert(int, CollationPropertyFromID(c.collationid, 'oledbcompstyle')),
  14472.         COLUMN_SORTID = case /* hack to keep the old behavior: will be removed */
  14473.                             when ABS(c.collationid) > 0x1000000 
  14474.                             then convert(int, DATABASEPROPERTYEX(db_name(),'sqlsortorder'))
  14475.                             else null 
  14476.                         end,
  14477.         
  14478.         COLUMN_TDSCOLLATION = convert (binary(5),CollationPropertyFromID(c.collationid, 'TDSCollation')),
  14479.         IS_COMPUTED = convert(bit, c.iscomputed)
  14480.     
  14481.     from
  14482.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  14483.         sysobjects o,
  14484.         master.dbo.spt_provider_types d,
  14485.         systypes t,
  14486.         master.dbo.syscharsets      a_cha /* charset/1001, not sortorder.*/
  14487.     where
  14488.         permissions(o.id, c.name) <> 0
  14489.     and     (o.type in ('U','V','S') OR (o.type in ('TF', 'IF') and c.number = 0))
  14490.     and (@table_schema is null or @table_schema = user_name(o.uid))
  14491.     and (@column_name is null or @column_name = c.name)
  14492.     and     o.id = c.id
  14493.     and     t.xtype = d.ss_dtype
  14494.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  14495.     and     c.xusertype = t.xusertype
  14496.     and a_cha.type = 1001 /* type is charset */
  14497.     and a_cha.id = isnull(convert(tinyint, CollationPropertyFromID(c.collationid, 'sqlcharset')),
  14498.             convert(tinyint, ServerProperty('sqlcharset'))) -- make sure there's one and only one row selected for each column
  14499.     order by 2, 3, c.colorder
  14500. go
  14501.  
  14502. dump tran master with no_log
  14503. go
  14504.  
  14505. /*  Procedure for 8.0 server */
  14506. create procedure sp_columns_rowset;5
  14507.     (
  14508.     @table_server       sysname,
  14509.     @table_catalog      sysname = null,
  14510.     @table_name     sysname = null,
  14511.     @table_schema       sysname = null,
  14512.     @column_name        sysname = null
  14513.     )
  14514. as
  14515.     select
  14516.         TABLE_CATALOG,
  14517.         TABLE_SCHEMA,
  14518.         TABLE_NAME,
  14519.         COLUMN_NAME,
  14520.         COLUMN_GUID,
  14521.         COLUMN_PROPID,
  14522.         ORDINAL_POSITION,
  14523.         COLUMN_HASDEFAULT,
  14524.         COLUMN_DEFAULT,
  14525.         COLUMN_FLAGS,
  14526.         IS_NULLABLE,
  14527.         DATA_TYPE,
  14528.         TYPE_GUID,
  14529.         CHARACTER_MAXIMUM_LENGTH,
  14530.         CHARACTER_OCTET_LENGTH,
  14531.         NUMERIC_PRECISION,
  14532.         NUMERIC_SCALE,
  14533.         DATETIME_PRECISION,
  14534.         CHARACTER_SET_CATALOG,
  14535.         CHARACTER_SET_SCHEMA,
  14536.         CHARACTER_SET_NAME,
  14537.         COLLATION_CATALOG,
  14538.         COLLATION_SCHEMA,
  14539.         COLLATION_NAME,
  14540.         DOMAIN_CATALOG,
  14541.         DOMAIN_SCHEMA,
  14542.         DOMAIN_NAME,
  14543.         DESCRIPTION
  14544.  
  14545.     from master.dbo.SYSREMOTE_COLUMNS <
  14546.                 @table_server,
  14547.                 @table_catalog,
  14548.                 @table_schema,
  14549.                 @table_name,
  14550.                 @column_name > 
  14551.     order by 1, 2, 3, 7
  14552. go
  14553.  
  14554.  
  14555. grant execute on sp_columns_rowset to public
  14556. go
  14557.  
  14558. dump tran master with no_log
  14559. go
  14560. if (charindex('6.00', @@version) > 0)
  14561.     begin
  14562.     if (exists (select * from sysobjects
  14563.             where name = 'sp_columns_rowset' and type = 'P '))
  14564.         begin
  14565.         drop procedure sp_columns_rowset
  14566.         dump tran master with no_log
  14567.         end
  14568.     end
  14569. go
  14570.  
  14571. print ''
  14572. print 'creating sp_check_constraints_rowset'
  14573. go
  14574.  
  14575. /*  Procedure for 6.50 and earlier servers */
  14576. create procedure sp_check_constraints_rowset
  14577.     (
  14578.     @constraint_name    varchar(255),
  14579.     @constraint_schema  varchar(255) = null
  14580.     )
  14581. as
  14582.     select
  14583.         CONSTRAINT_CATALOG  = db_name(),
  14584.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  14585.         CONSTRAINT_NAME     = c_obj.name,
  14586.         CHECK_CLAUSE        = m.text,   
  14587.         DESCRIPTION         = convert(varchar(1),null)
  14588.     from 
  14589.         sysobjects c_obj, syscomments m
  14590.     where
  14591.             c_obj.type = 'C '
  14592.         and c_obj.name  = @constraint_name
  14593.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14594.         and c_obj.id = m.id
  14595.     order by 1,2,3 
  14596. go
  14597. dump tran master with no_log
  14598. go
  14599.  
  14600. create procedure sp_check_constraints_rowset;2
  14601.     (
  14602.     @constraint_schema  varchar(255) = null
  14603.     )
  14604. as
  14605.     select
  14606.         CONSTRAINT_CATALOG  = db_name(),
  14607.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  14608.         CONSTRAINT_NAME     = c_obj.name,
  14609.         CHECK_CLAUSE        = m.text,   
  14610.         DESCRIPTION         = convert(varchar(1),null)
  14611.     from 
  14612.         sysobjects c_obj, syscomments m
  14613.     where
  14614.             c_obj.type = 'C '
  14615.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14616.         and c_obj.id = m.id
  14617.     order by 1,2,3 
  14618. go
  14619. dump tran master with no_log
  14620. go
  14621.  
  14622. if (charindex('7.00', @@version) > 0 or
  14623.     charindex('8.00', @@version) > 0)
  14624.     drop procedure sp_check_constraints_rowset
  14625. else
  14626. begin
  14627.     print ''
  14628.     print ''
  14629.     print 'Warning:'
  14630.     print 'you are installing the stored procedures '
  14631.     print 'on a pre 7.0 SQL Server.'
  14632.     print 'Ignore the following errors.'
  14633. end
  14634. go
  14635.  
  14636. /*  Procedure for 7.0 servers and 8.0 servers */
  14637. create procedure sp_check_constraints_rowset
  14638.     (
  14639.     @constraint_name    sysname,
  14640.     @constraint_schema  sysname = null
  14641.     )
  14642. as
  14643.     select
  14644.         CONSTRAINT_CATALOG  = db_name(),
  14645.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  14646.         CONSTRAINT_NAME     = c_obj.name,
  14647.         CHECK_CLAUSE        = m.text,   
  14648.         DESCRIPTION         = convert(nvarchar(1), null)
  14649.     from 
  14650.         sysobjects c_obj, syscomments m
  14651.     where
  14652.             c_obj.type = 'C '
  14653.         and c_obj.name  = @constraint_name
  14654.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14655.         and m.id = c_obj.id
  14656.     order by 1,2,3 
  14657. go
  14658. dump tran master with no_log
  14659. go
  14660.  
  14661. create procedure sp_check_constraints_rowset;2
  14662.     (
  14663.     @constraint_schema  sysname = null
  14664.     )
  14665. as
  14666.     select
  14667.         CONSTRAINT_CATALOG  = db_name(),
  14668.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  14669.         CONSTRAINT_NAME     = c_obj.name,
  14670.         CHECK_CLAUSE        = m.text,   
  14671.         DESCRIPTION         = convert(nvarchar(1), null)
  14672.     from 
  14673.         sysobjects c_obj, syscomments m
  14674.     where
  14675.             c_obj.type = 'C '
  14676.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14677.         and m.id = c_obj.id
  14678.     order by 1,2,3 
  14679. go
  14680.  
  14681. grant execute on sp_check_constraints_rowset to public
  14682. go
  14683.  
  14684. dump tran master with no_log
  14685. go
  14686.  
  14687. if (charindex('6.00', @@version) > 0)
  14688.     begin
  14689.     if (exists (select * from sysobjects
  14690.             where name = 'sp_check_constraints_rowset' and type = 'P '))
  14691.         begin
  14692.         drop procedure sp_check_constraints_rowset
  14693.         dump tran master with no_log
  14694.         end
  14695.     end
  14696. go
  14697.  
  14698. print ''
  14699. print 'creating sp_check_constbytable_rowset'
  14700. go
  14701.  
  14702. /*  Procedure for 6.50 and earlier servers */
  14703. create procedure sp_check_constbytable_rowset
  14704.     (
  14705.     @table_name         varchar(255), 
  14706.     @table_schema       varchar(255) = null,
  14707.     @constraint_name    varchar(255) = null,
  14708.     @constraint_schema  varchar(255) = null
  14709.     )
  14710. as
  14711.     select
  14712.         TABLE_CATALOG       = db_name(),
  14713.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14714.         TABLE_NAME          = t_obj.name,
  14715.         CONSTRAINT_CATALOG  = db_name(),
  14716.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  14717.         CONSTRAINT_NAME     = c_obj.name,
  14718.         CHECK_CLAUSE        = m.text,   
  14719.         DESCRIPTION         = convert(varchar(1),null)
  14720.     from 
  14721.         sysobjects c_obj, sysobjects t_obj, sysconstraints c, syscomments m
  14722.     where
  14723.             t_obj.name  = @table_name
  14724.         and t_obj.type in ('U ','S ')
  14725.         and (@table_schema is null or @table_schema = user_name(t_obj.uid)) 
  14726.         and t_obj.id = c.id
  14727.         and c.constid = c_obj.id
  14728.         and c_obj.type = 'C '
  14729.         and (@constraint_name is null or c_obj.name = @constraint_name)
  14730.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14731.         and c_obj.id = m.id
  14732.     order by 1,2,3,4,5,6 
  14733. go
  14734. dump tran master with no_log
  14735. go
  14736.  
  14737. create procedure sp_check_constbytable_rowset;2
  14738.     (
  14739.     @table_schema       varchar(255) = null,
  14740.     @constraint_name    varchar(255) = null,
  14741.     @constraint_schema  varchar(255) = null
  14742.     )
  14743. as
  14744.     select
  14745.         TABLE_CATALOG       = db_name(),
  14746.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14747.         TABLE_NAME          = t_obj.name,
  14748.         CONSTRAINT_CATALOG  = db_name(),
  14749.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  14750.         CONSTRAINT_NAME     = c_obj.name,
  14751.         CHECK_CLAUSE        = m.text,   
  14752.         DESCRIPTION         = convert(varchar(1),null)
  14753.     from 
  14754.         sysobjects c_obj, sysobjects t_obj, sysconstraints c, syscomments m
  14755.     where
  14756.             t_obj.type in ('U ','S ')
  14757.         and (@table_schema is null or @table_schema = user_name(t_obj.uid))
  14758.         and c.id = t_obj.id
  14759.         and c.constid = c_obj.id
  14760.         and c_obj.type  = 'C '
  14761.         and (@constraint_name is null or c_obj.name = @constraint_name)
  14762.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14763.         and c_obj.id = m.id
  14764.     order by 1,2,3,4,5,6 
  14765. go
  14766. dump tran master with no_log
  14767. go
  14768.  
  14769. if (charindex('7.00', @@version) > 0 or
  14770.     charindex('8.00', @@version) > 0)
  14771.     drop procedure sp_check_constbytable_rowset
  14772. else
  14773. begin
  14774.     print ''
  14775.     print ''
  14776.     print 'Warning:'
  14777.     print 'you are installing the stored procedures '
  14778.     print 'on a pre 8.0 SQL Server.'
  14779.     print 'Ignore the following errors.'
  14780. end
  14781. go
  14782.  
  14783. /*  Procedure for 8.0 server */
  14784. create procedure sp_check_constbytable_rowset
  14785.     (
  14786.     @table_name         sysname, 
  14787.     @table_schema       sysname = null,
  14788.     @constraint_name    sysname = null,
  14789.     @constraint_schema  sysname = null
  14790.     )
  14791. as
  14792.     select
  14793.         TABLE_CATALOG       = db_name(),
  14794.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14795.         TABLE_NAME          = t_obj.name,
  14796.         CONSTRAINT_CATALOG  = db_name(),
  14797.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  14798.         CONSTRAINT_NAME     = c_obj.name,
  14799.         CHECK_CLAUSE        = m.text,   
  14800.         DESCRIPTION         = convert(nvarchar(1), null)
  14801.  
  14802.     from 
  14803.         sysobjects c_obj, sysobjects t_obj, syscomments m
  14804.     where
  14805.             t_obj.name  = @table_name
  14806.         and t_obj.type in ('U ','S ')
  14807.         and (@table_schema is null or @table_schema = user_name(t_obj.uid))
  14808.         and c_obj.parent_obj = t_obj.id
  14809.         and c_obj.type = 'C '
  14810.         and (@constraint_name is null or c_obj.name = @constraint_name)
  14811.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14812.         and m.id = c_obj.id
  14813. order by 1,2,3,4,5,6 
  14814. go
  14815. dump tran master with no_log
  14816. go
  14817. create procedure sp_check_constbytable_rowset;2
  14818.     (
  14819.     @table_schema       sysname = null,
  14820.     @constraint_name    sysname = null,
  14821.     @constraint_schema  sysname = null
  14822.     )
  14823. as
  14824.     select
  14825.         TABLE_CATALOG       = db_name(),
  14826.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14827.         TABLE_NAME          = t_obj.name,
  14828.         CONSTRAINT_CATALOG  = db_name(),
  14829.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  14830.         CONSTRAINT_NAME     = c_obj.name,
  14831.         CHECK_CLAUSE        = m.text,   
  14832.         DESCRIPTION         = convert(nvarchar(1), null)
  14833.     from 
  14834.         sysobjects c_obj, sysobjects t_obj, syscomments m
  14835.     where
  14836.             t_obj.type in ('U ','S ')
  14837.         and (@table_schema is null or @table_schema = user_name(t_obj.uid))
  14838.         and c_obj.parent_obj = t_obj.id
  14839.         and c_obj.type = 'C '
  14840.         and (@constraint_name is null or c_obj.name = @constraint_name)
  14841.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14842.         and m.id = c_obj.id
  14843.     order by 1,2,3,4,5,6 
  14844. go
  14845. dump tran master with no_log
  14846. go
  14847.  
  14848. grant execute on sp_check_constbytable_rowset to public
  14849. go
  14850.  
  14851. dump tran master with no_log
  14852. go
  14853. if (charindex('6.00', @@version) > 0)
  14854.     begin
  14855.     if (exists (select * from sysobjects
  14856.             where name = 'sp_check_constbytable_rowset' and type = 'P '))
  14857.         begin
  14858.         drop procedure sp_check_constbytable_rowset
  14859.         dump tran master with no_log
  14860.         end
  14861.     end
  14862. go
  14863.  
  14864.  
  14865. print ''
  14866. print 'creating sp_foreign_keys_rowset'
  14867. go
  14868.  
  14869. /*  Procedure for 6.0 and 6.5 server */
  14870. CREATE PROCEDURE sp_foreign_keys_rowset
  14871.     (
  14872.     @pk_table_name      varchar(255) = null,
  14873.     @pk_table_schema    varchar(255) = null,
  14874.     @pk_table_catalog   varchar(255) = null,
  14875.     @fk_table_name      varchar(255) = null,
  14876.     @fk_table_schema    varchar(255) = null,
  14877.     @fk_table_catalog   varchar(255) = null
  14878.     )
  14879. as
  14880.     BEGIN
  14881.     select
  14882.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14883.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  14884.         PK_TABLE_NAME       = o1.name,
  14885.         PK_COLUMN_NAME      = c1.name,
  14886.         PK_COLUMN_GUID      = convert(binary(16),null),
  14887.         PK_COLUMN_PROPID    = convert(int,null),
  14888.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14889.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  14890.         FK_TABLE_NAME       = o2.name,
  14891.         FK_COLUMN_NAME      = c2.name,
  14892.         FK_COLUMN_GUID      = convert(binary(16),null),
  14893.         FK_COLUMN_PROPID    = convert(int,null),
  14894.         ORDINAL             = convert(int,1),
  14895.         UPDATE_RULE         = 'NO ACTION',
  14896.         DELETE_RULE         = 'NO ACTION',
  14897.         PK_NAME             = i.name,
  14898.         FK_NAME             = object_name(r.constid),
  14899.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14900.     from
  14901.         sysobjects o1, sysobjects o2,
  14902.         syscolumns c1, syscolumns c2,
  14903.         sysreferences r, sysindexes i, sysusers u
  14904.     where   
  14905.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14906.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14907.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14908.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14909.     and (@fk_table_name is null or @fk_table_name = o2.name)
  14910.     and o1.id = r.rkeyid
  14911.     and o1.id = c1.id
  14912.     and c1.colid = r.rkey1
  14913.     and r.fkeyid = o2.id
  14914.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14915.     and o2.id = c2.id   
  14916.     and c2.colid = r.fkey1
  14917.     and i.id = r.rkeyid
  14918.     and i.indid = r.rkeyindid
  14919.     and u.uid = user_id()
  14920.     and ( suser_id() = 1     /* User is the System Administrator */
  14921.     or (
  14922.         o1.uid = user_id()   /* User created the object */
  14923.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14924.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14925.             from sysprotects p
  14926.             where p.id = o1.id
  14927.             /* get rows for public,current user,user's group */
  14928.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14929.             /* check for SELECT,EXECUTE privilege */
  14930.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14931.             ) = 1   /* final magic...compare Grants   */
  14932.         )
  14933.     and (
  14934.         o2.uid = user_id()   /* User created the object */
  14935.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14936.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14937.             from sysprotects p
  14938.             where p.id = o2.id
  14939.             /* get rows for public,current user,user's group */
  14940.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14941.             /* check for SELECT,EXECUTE privilege */
  14942.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14943.             ) = 1   /* final magic...compare Grants   */
  14944.         )
  14945.     )
  14946.     union all
  14947.     select
  14948.         db_name(r.rkeydbid),
  14949.         user_name(o1.uid),
  14950.         o1.name,
  14951.         c1.name,
  14952.         convert(binary(16),null),
  14953.         convert(int,null),
  14954.         db_name(r.fkeydbid),
  14955.         user_name(o2.uid),
  14956.         o2.name,
  14957.         c2.name,
  14958.         convert(binary(16),null),
  14959.         convert(int,null),
  14960.         convert(int,2),
  14961.         'NO ACTION',
  14962.         'NO ACTION',
  14963.         i.name,
  14964.         object_name(r.constid),
  14965.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14966.     from
  14967.         sysobjects o1, sysobjects o2,
  14968.         syscolumns c1, syscolumns c2,
  14969.         sysreferences r, sysindexes i, sysusers u
  14970.     where   
  14971.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14972.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14973.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14974.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14975.     and (@fk_table_name is null or @fk_table_name = o2.name)
  14976.     and o1.id = r.rkeyid
  14977.     and r.keycnt >= 2
  14978.     and o1.id = c1.id
  14979.     and c1.colid = r.rkey2
  14980.     and r.fkeyid = o2.id
  14981.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14982.     and o2.id = c2.id   
  14983.     and c2.colid = r.fkey2
  14984.     and i.id = r.rkeyid
  14985.     and i.indid = r.rkeyindid
  14986.     and u.uid = user_id()
  14987.     and ( suser_id() = 1     /* User is the System Administrator */
  14988.     or (
  14989.         o1.uid = user_id()   /* User created the object */
  14990.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14991.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14992.             from sysprotects p
  14993.             where p.id = o1.id
  14994.             /* get rows for public,current user,user's group */
  14995.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14996.             /* check for SELECT,EXECUTE privilege */
  14997.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14998.             ) = 1   /* final magic...compare Grants   */
  14999.         )
  15000.     and (
  15001.         o2.uid = user_id()   /* User created the object */
  15002.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15003.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15004.             from sysprotects p
  15005.             where p.id = o2.id
  15006.             /* get rows for public,current user,user's group */
  15007.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15008.             /* check for SELECT,EXECUTE privilege */
  15009.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15010.             ) = 1   /* final magic...compare Grants   */
  15011.         )
  15012.     )
  15013.     union all
  15014.     select
  15015.         db_name(r.rkeydbid),
  15016.         user_name(o1.uid),
  15017.         o1.name,
  15018.         c1.name,
  15019.         convert(binary(16),null),
  15020.         convert(int,null),
  15021.         db_name(r.fkeydbid),
  15022.         user_name(o2.uid),
  15023.         o2.name,
  15024.         c2.name,
  15025.         convert(binary(16),null),
  15026.         convert(int,null),
  15027.         convert(int,3),
  15028.         'NO ACTION',
  15029.         'NO ACTION',
  15030.         i.name,
  15031.         object_name(r.constid),
  15032.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15033.     from
  15034.         sysobjects o1, sysobjects o2,
  15035.         syscolumns c1, syscolumns c2,
  15036.         sysreferences r, sysindexes i, sysusers u
  15037.     where   
  15038.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15039.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15040.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15041.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15042.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15043.     and o1.id = r.rkeyid
  15044.     and r.keycnt >= 3
  15045.     and o1.id = c1.id
  15046.     and c1.colid = r.rkey3
  15047.     and r.fkeyid = o2.id
  15048.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15049.     and o2.id = c2.id   
  15050.     and c2.colid = r.fkey3
  15051.     and i.id = r.rkeyid
  15052.     and i.indid = r.rkeyindid
  15053.     and u.uid = user_id()
  15054.     and ( suser_id() = 1     /* User is the System Administrator */
  15055.     or (
  15056.         o1.uid = user_id()   /* User created the object */
  15057.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15058.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15059.             from sysprotects p
  15060.             where p.id = o1.id
  15061.             /* get rows for public,current user,user's group */
  15062.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15063.             /* check for SELECT,EXECUTE privilege */
  15064.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15065.             ) = 1   /* final magic...compare Grants   */
  15066.         )
  15067.     and (
  15068.         o2.uid = user_id()   /* User created the object */
  15069.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15070.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15071.             from sysprotects p
  15072.             where p.id = o2.id
  15073.             /* get rows for public,current user,user's group */
  15074.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15075.             /* check for SELECT,EXECUTE privilege */
  15076.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15077.             ) = 1   /* final magic...compare Grants   */
  15078.         )
  15079.     )
  15080.     union all
  15081.     select
  15082.         db_name(r.rkeydbid),
  15083.         user_name(o1.uid),
  15084.         o1.name,
  15085.         c1.name,
  15086.         convert(binary(16),null),
  15087.         convert(int,null),
  15088.         db_name(r.fkeydbid),
  15089.         user_name(o2.uid),
  15090.         o2.name,
  15091.         c2.name,
  15092.         convert(binary(16),null),
  15093.         convert(int,null),
  15094.         convert(int,4),
  15095.         'NO ACTION',
  15096.         'NO ACTION',
  15097.         i.name,
  15098.         object_name(r.constid),
  15099.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15100.     from
  15101.         sysobjects o1, sysobjects o2,
  15102.         syscolumns c1, syscolumns c2,
  15103.         sysreferences r, sysindexes i, sysusers u
  15104.     where   
  15105.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15106.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15107.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15108.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15109.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15110.     and o1.id = r.rkeyid
  15111.     and r.keycnt >= 4
  15112.     and o1.id = c1.id
  15113.     and c1.colid = r.rkey4
  15114.     and r.fkeyid = o2.id
  15115.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15116.     and o2.id = c2.id   
  15117.     and c2.colid = r.fkey4
  15118.     and i.id = r.rkeyid
  15119.     and i.indid = r.rkeyindid
  15120.     and u.uid = user_id()
  15121.     and ( suser_id() = 1     /* User is the System Administrator */
  15122.     or (
  15123.         o1.uid = user_id()   /* User created the object */
  15124.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15125.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15126.             from sysprotects p
  15127.             where p.id = o1.id
  15128.             /* get rows for public,current user,user's group */
  15129.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15130.             /* check for SELECT,EXECUTE privilege */
  15131.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15132.             ) = 1   /* final magic...compare Grants   */
  15133.         )
  15134.     and (
  15135.         o2.uid = user_id()   /* User created the object */
  15136.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15137.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15138.             from sysprotects p
  15139.             where p.id = o2.id
  15140.             /* get rows for public,current user,user's group */
  15141.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15142.             /* check for SELECT,EXECUTE privilege */
  15143.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15144.             ) = 1   /* final magic...compare Grants   */
  15145.         )
  15146.     )
  15147.     union all
  15148.     select
  15149.         db_name(r.rkeydbid),
  15150.         user_name(o1.uid),
  15151.         o1.name,
  15152.         c1.name,
  15153.         convert(binary(16),null),
  15154.         convert(int,null),
  15155.         db_name(r.fkeydbid),
  15156.         user_name(o2.uid),
  15157.         o2.name,
  15158.         c2.name,
  15159.         convert(binary(16),null),
  15160.         convert(int,null),
  15161.         convert(int,5),
  15162.         'NO ACTION',
  15163.         'NO ACTION',
  15164.         i.name,
  15165.         object_name(r.constid),
  15166.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15167.     from
  15168.         sysobjects o1, sysobjects o2,
  15169.         syscolumns c1, syscolumns c2,
  15170.         sysreferences r, sysindexes i, sysusers u
  15171.     where   
  15172.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15173.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15174.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15175.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15176.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15177.     and o1.id = r.rkeyid
  15178.     and r.keycnt >= 5
  15179.     and o1.id = c1.id
  15180.     and c1.colid = r.rkey5
  15181.     and r.fkeyid = o2.id
  15182.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15183.     and o2.id = c2.id   
  15184.     and c2.colid = r.fkey5
  15185.     and i.id = r.rkeyid
  15186.     and i.indid = r.rkeyindid
  15187.     and u.uid = user_id()
  15188.     and ( suser_id() = 1     /* User is the System Administrator */
  15189.     or (
  15190.         o1.uid = user_id()   /* User created the object */
  15191.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15192.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15193.             from sysprotects p
  15194.             where p.id = o1.id
  15195.             /* get rows for public,current user,user's group */
  15196.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15197.             /* check for SELECT,EXECUTE privilege */
  15198.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15199.             ) = 1   /* final magic...compare Grants   */
  15200.         )
  15201.     and (
  15202.         o2.uid = user_id()   /* User created the object */
  15203.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15204.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15205.             from sysprotects p
  15206.             where p.id = o2.id
  15207.             /* get rows for public,current user,user's group */
  15208.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15209.             /* check for SELECT,EXECUTE privilege */
  15210.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15211.             ) = 1   /* final magic...compare Grants   */
  15212.         )
  15213.     )
  15214.     union all
  15215.     select
  15216.         db_name(r.rkeydbid),
  15217.         user_name(o1.uid),
  15218.         o1.name,
  15219.         c1.name,
  15220.         convert(binary(16),null),
  15221.         convert(int,null),
  15222.         db_name(r.fkeydbid),
  15223.         user_name(o2.uid),
  15224.         o2.name,
  15225.         c2.name,
  15226.         convert(binary(16),null),
  15227.         convert(int,null),
  15228.         convert(int,6),
  15229.         'NO ACTION',
  15230.         'NO ACTION',
  15231.         i.name,
  15232.         object_name(r.constid),
  15233.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15234.     from
  15235.         sysobjects o1, sysobjects o2,
  15236.         syscolumns c1, syscolumns c2,
  15237.         sysreferences r, sysindexes i, sysusers u
  15238.     where   
  15239.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15240.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15241.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15242.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15243.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15244.     and o1.id = r.rkeyid
  15245.     and r.keycnt >= 6
  15246.     and o1.id = c1.id
  15247.     and c1.colid = r.rkey6
  15248.     and r.fkeyid = o2.id
  15249.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15250.     and o2.id = c2.id   
  15251.     and c2.colid = r.fkey6
  15252.     and i.id = r.rkeyid
  15253.     and i.indid = r.rkeyindid
  15254.     and u.uid = user_id()
  15255.     and ( suser_id() = 1     /* User is the System Administrator */
  15256.     or (
  15257.         o1.uid = user_id()   /* User created the object */
  15258.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15259.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15260.             from sysprotects p
  15261.             where p.id = o1.id
  15262.             /* get rows for public,current user,user's group */
  15263.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15264.             /* check for SELECT,EXECUTE privilege */
  15265.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15266.             ) = 1   /* final magic...compare Grants   */
  15267.         )
  15268.     and (
  15269.         o2.uid = user_id()   /* User created the object */
  15270.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15271.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15272.             from sysprotects p
  15273.             where p.id = o2.id
  15274.             /* get rows for public,current user,user's group */
  15275.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15276.             /* check for SELECT,EXECUTE privilege */
  15277.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15278.             ) = 1   /* final magic...compare Grants   */
  15279.         )
  15280.     )
  15281.     union all
  15282.     select
  15283.         db_name(r.rkeydbid),
  15284.         user_name(o1.uid),
  15285.         o1.name,
  15286.         c1.name,
  15287.         convert(binary(16),null),
  15288.         convert(int,null),
  15289.         db_name(r.fkeydbid),
  15290.         user_name(o2.uid),
  15291.         o2.name,
  15292.         c2.name,
  15293.         convert(binary(16),null),
  15294.         convert(int,null),
  15295.         convert(int,7),
  15296.         'NO ACTION',
  15297.         'NO ACTION',
  15298.         i.name,
  15299.         object_name(r.constid),
  15300.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15301.     from
  15302.         sysobjects o1, sysobjects o2,
  15303.         syscolumns c1, syscolumns c2,
  15304.         sysreferences r, sysindexes i, sysusers u
  15305.     where   
  15306.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15307.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15308.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15309.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15310.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15311.     and o1.id = r.rkeyid
  15312.     and r.keycnt >= 7
  15313.     and o1.id = c1.id
  15314.     and c1.colid = r.rkey7
  15315.     and r.fkeyid = o2.id
  15316.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15317.     and o2.id = c2.id   
  15318.     and c2.colid = r.fkey7
  15319.     and i.id = r.rkeyid
  15320.     and i.indid = r.rkeyindid
  15321.     and u.uid = user_id()
  15322.     and ( suser_id() = 1     /* User is the System Administrator */
  15323.     or (
  15324.         o1.uid = user_id()   /* User created the object */
  15325.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15326.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15327.             from sysprotects p
  15328.             where p.id = o1.id
  15329.             /* get rows for public,current user,user's group */
  15330.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15331.             /* check for SELECT,EXECUTE privilege */
  15332.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15333.             ) = 1   /* final magic...compare Grants   */
  15334.         )
  15335.     and (
  15336.         o2.uid = user_id()   /* User created the object */
  15337.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15338.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15339.             from sysprotects p
  15340.             where p.id = o2.id
  15341.             /* get rows for public,current user,user's group */
  15342.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15343.             /* check for SELECT,EXECUTE privilege */
  15344.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15345.             ) = 1   /* final magic...compare Grants   */
  15346.         )
  15347.     )
  15348.     union all
  15349.     select
  15350.         db_name(r.rkeydbid),
  15351.         user_name(o1.uid),
  15352.         o1.name,
  15353.         c1.name,
  15354.         convert(binary(16),null),
  15355.         convert(int,null),
  15356.         db_name(r.fkeydbid),
  15357.         user_name(o2.uid),
  15358.         o2.name,
  15359.         c2.name,
  15360.         convert(binary(16),null),
  15361.         convert(int,null),
  15362.         convert(int,8),
  15363.         'NO ACTION',
  15364.         'NO ACTION',
  15365.         i.name,
  15366.         object_name(r.constid),
  15367.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15368.     from
  15369.         sysobjects o1, sysobjects o2,
  15370.         syscolumns c1, syscolumns c2,
  15371.         sysreferences r, sysindexes i, sysusers u
  15372.     where   
  15373.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15374.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15375.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15376.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15377.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15378.     and o1.id = r.rkeyid
  15379.     and r.keycnt >= 8
  15380.     and o1.id = c1.id
  15381.     and c1.colid = r.rkey8
  15382.     and r.fkeyid = o2.id
  15383.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15384.     and o2.id = c2.id   
  15385.     and c2.colid = r.fkey8
  15386.     and i.id = r.rkeyid
  15387.     and i.indid = r.rkeyindid
  15388.     and u.uid = user_id()
  15389.     and ( suser_id() = 1     /* User is the System Administrator */
  15390.     or (
  15391.         o1.uid = user_id()   /* User created the object */
  15392.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15393.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15394.             from sysprotects p
  15395.             where p.id = o1.id
  15396.             /* get rows for public,current user,user's group */
  15397.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15398.             /* check for SELECT,EXECUTE privilege */
  15399.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15400.             ) = 1   /* final magic...compare Grants   */
  15401.         )
  15402.     and (
  15403.         o2.uid = user_id()   /* User created the object */
  15404.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15405.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15406.             from sysprotects p
  15407.             where p.id = o2.id
  15408.             /* get rows for public,current user,user's group */
  15409.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15410.             /* check for SELECT,EXECUTE privilege */
  15411.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15412.             ) = 1   /* final magic...compare Grants   */
  15413.         )
  15414.     )
  15415.     union all
  15416.     select
  15417.         db_name(r.rkeydbid),
  15418.         user_name(o1.uid),
  15419.         o1.name,
  15420.         c1.name,
  15421.         convert(binary(16),null),
  15422.         convert(int,null),
  15423.         db_name(r.fkeydbid),
  15424.         user_name(o2.uid),
  15425.         o2.name,
  15426.         c2.name,
  15427.         convert(binary(16),null),
  15428.         convert(int,null),
  15429.         convert(int,9),
  15430.         'NO ACTION',
  15431.         'NO ACTION',
  15432.         i.name,
  15433.         object_name(r.constid),
  15434.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15435.     from
  15436.         sysobjects o1, sysobjects o2,
  15437.         syscolumns c1, syscolumns c2,
  15438.         sysreferences r, sysindexes i, sysusers u
  15439.     where   
  15440.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15441.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15442.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15443.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15444.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15445.     and o1.id = r.rkeyid
  15446.     and r.keycnt >= 9
  15447.     and o1.id = c1.id
  15448.     and c1.colid = r.rkey9
  15449.     and r.fkeyid = o2.id
  15450.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15451.     and o2.id = c2.id   
  15452.     and c2.colid = r.fkey9
  15453.     and i.id = r.rkeyid
  15454.     and i.indid = r.rkeyindid
  15455.     and u.uid = user_id()
  15456.     and ( suser_id() = 1     /* User is the System Administrator */
  15457.     or (
  15458.         o1.uid = user_id()   /* User created the object */
  15459.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15460.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15461.             from sysprotects p
  15462.             where p.id = o1.id
  15463.             /* get rows for public,current user,user's group */
  15464.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15465.             /* check for SELECT,EXECUTE privilege */
  15466.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15467.             ) = 1   /* final magic...compare Grants   */
  15468.         )
  15469.     and (
  15470.         o2.uid = user_id()   /* User created the object */
  15471.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15472.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15473.             from sysprotects p
  15474.             where p.id = o2.id
  15475.             /* get rows for public,current user,user's group */
  15476.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15477.             /* check for SELECT,EXECUTE privilege */
  15478.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15479.             ) = 1   /* final magic...compare Grants   */
  15480.         )
  15481.     )
  15482.     union all
  15483.     select
  15484.         db_name(r.rkeydbid),
  15485.         user_name(o1.uid),
  15486.         o1.name,
  15487.         c1.name,
  15488.         convert(binary(16),null),
  15489.         convert(int,null),
  15490.         db_name(r.fkeydbid),
  15491.         user_name(o2.uid),
  15492.         o2.name,
  15493.         c2.name,
  15494.         convert(binary(16),null),
  15495.         convert(int,null),
  15496.         convert(int,10),
  15497.         'NO ACTION',
  15498.         'NO ACTION',
  15499.         i.name,
  15500.         object_name(r.constid),
  15501.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15502.     from
  15503.         sysobjects o1, sysobjects o2,
  15504.         syscolumns c1, syscolumns c2,
  15505.         sysreferences r, sysindexes i, sysusers u
  15506.     where   
  15507.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15508.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15509.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15510.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15511.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15512.     and o1.id = r.rkeyid
  15513.     and r.keycnt >= 10
  15514.     and o1.id = c1.id
  15515.     and c1.colid = r.rkey10
  15516.     and r.fkeyid = o2.id
  15517.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15518.     and o2.id = c2.id   
  15519.     and c2.colid = r.fkey10
  15520.     and i.id = r.rkeyid
  15521.     and i.indid = r.rkeyindid
  15522.     and u.uid = user_id()
  15523.     and ( suser_id() = 1     /* User is the System Administrator */
  15524.     or (
  15525.         o1.uid = user_id()   /* User created the object */
  15526.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15527.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15528.             from sysprotects p
  15529.             where p.id = o1.id
  15530.             /* get rows for public,current user,user's group */
  15531.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15532.             /* check for SELECT,EXECUTE privilege */
  15533.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15534.             ) = 1   /* final magic...compare Grants   */
  15535.         )
  15536.     and (
  15537.         o2.uid = user_id()   /* User created the object */
  15538.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15539.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15540.             from sysprotects p
  15541.             where p.id = o2.id
  15542.             /* get rows for public,current user,user's group */
  15543.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15544.             /* check for SELECT,EXECUTE privilege */
  15545.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15546.             ) = 1   /* final magic...compare Grants   */
  15547.         )
  15548.     )
  15549.     union all
  15550.     select
  15551.         db_name(r.rkeydbid),
  15552.         user_name(o1.uid),
  15553.         o1.name,
  15554.         c1.name,
  15555.         convert(binary(16),null),
  15556.         convert(int,null),
  15557.         db_name(r.fkeydbid),
  15558.         user_name(o2.uid),
  15559.         o2.name,
  15560.         c2.name,
  15561.         convert(binary(16),null),
  15562.         convert(int,null),
  15563.         convert(int,11),
  15564.         'NO ACTION',
  15565.         'NO ACTION',
  15566.         i.name,
  15567.         object_name(r.constid),
  15568.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15569.     from
  15570.         sysobjects o1, sysobjects o2,
  15571.         syscolumns c1, syscolumns c2,
  15572.         sysreferences r, sysindexes i, sysusers u
  15573.     where   
  15574.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15575.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15576.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15577.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15578.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15579.     and o1.id = r.rkeyid
  15580.     and r.keycnt >= 11
  15581.     and o1.id = c1.id
  15582.     and c1.colid = r.rkey11
  15583.     and r.fkeyid = o2.id
  15584.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15585.     and o2.id = c2.id   
  15586.     and c2.colid = r.fkey11
  15587.     and i.id = r.rkeyid
  15588.     and i.indid = r.rkeyindid
  15589.     and u.uid = user_id()
  15590.     and ( suser_id() = 1     /* User is the System Administrator */
  15591.     or (
  15592.         o1.uid = user_id()   /* User created the object */
  15593.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15594.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15595.             from sysprotects p
  15596.             where p.id = o1.id
  15597.             /* get rows for public,current user,user's group */
  15598.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15599.             /* check for SELECT,EXECUTE privilege */
  15600.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15601.             ) = 1   /* final magic...compare Grants   */
  15602.         )
  15603.     and (
  15604.         o2.uid = user_id()   /* User created the object */
  15605.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15606.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15607.             from sysprotects p
  15608.             where p.id = o2.id
  15609.             /* get rows for public,current user,user's group */
  15610.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15611.             /* check for SELECT,EXECUTE privilege */
  15612.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15613.             ) = 1   /* final magic...compare Grants   */
  15614.         )
  15615.     )
  15616.     union all
  15617.     select
  15618.         db_name(r.rkeydbid),
  15619.         user_name(o1.uid),
  15620.         o1.name,
  15621.         c1.name,
  15622.         convert(binary(16),null),
  15623.         convert(int,null),
  15624.         db_name(r.fkeydbid),
  15625.         user_name(o2.uid),
  15626.         o2.name,
  15627.         c2.name,
  15628.         convert(binary(16),null),
  15629.         convert(int,null),
  15630.         convert(int,12),
  15631.         'NO ACTION',
  15632.         'NO ACTION',
  15633.         i.name,
  15634.         object_name(r.constid),
  15635.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15636.     from
  15637.         sysobjects o1, sysobjects o2,
  15638.         syscolumns c1, syscolumns c2,
  15639.         sysreferences r, sysindexes i, sysusers u
  15640.     where   
  15641.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15642.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15643.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15644.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15645.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15646.     and o1.id = r.rkeyid
  15647.     and r.keycnt >= 12
  15648.     and o1.id = c1.id
  15649.     and c1.colid = r.rkey12
  15650.     and r.fkeyid = o2.id
  15651.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15652.     and o2.id = c2.id   
  15653.     and c2.colid = r.fkey12
  15654.     and i.id = r.rkeyid
  15655.     and i.indid = r.rkeyindid
  15656.     and u.uid = user_id()
  15657.     and ( suser_id() = 1     /* User is the System Administrator */
  15658.     or (
  15659.         o1.uid = user_id()   /* User created the object */
  15660.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15661.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15662.             from sysprotects p
  15663.             where p.id = o1.id
  15664.             /* get rows for public,current user,user's group */
  15665.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15666.             /* check for SELECT,EXECUTE privilege */
  15667.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15668.             ) = 1   /* final magic...compare Grants   */
  15669.         )
  15670.     and (
  15671.         o2.uid = user_id()   /* User created the object */
  15672.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15673.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15674.             from sysprotects p
  15675.             where p.id = o2.id
  15676.             /* get rows for public,current user,user's group */
  15677.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15678.             /* check for SELECT,EXECUTE privilege */
  15679.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15680.             ) = 1   /* final magic...compare Grants   */
  15681.         )
  15682.     )
  15683.     union all
  15684.     select
  15685.         db_name(r.rkeydbid),
  15686.         user_name(o1.uid),
  15687.         o1.name,
  15688.         c1.name,
  15689.         convert(binary(16),null),
  15690.         convert(int,null),
  15691.         db_name(r.fkeydbid),
  15692.         user_name(o2.uid),
  15693.         o2.name,
  15694.         c2.name,
  15695.         convert(binary(16),null),
  15696.         convert(int,null),
  15697.         convert(int,13),
  15698.         'NO ACTION',
  15699.         'NO ACTION',
  15700.         i.name,
  15701.         object_name(r.constid),
  15702.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15703.     from
  15704.         sysobjects o1, sysobjects o2,
  15705.         syscolumns c1, syscolumns c2,
  15706.         sysreferences r, sysindexes i, sysusers u
  15707.     where   
  15708.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15709.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15710.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15711.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15712.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15713.     and o1.id = r.rkeyid
  15714.     and r.keycnt >= 13
  15715.     and o1.id = c1.id
  15716.     and c1.colid = r.rkey13
  15717.     and r.fkeyid = o2.id
  15718.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15719.     and o2.id = c2.id   
  15720.     and c2.colid = r.fkey13
  15721.     and i.id = r.rkeyid
  15722.     and i.indid = r.rkeyindid
  15723.     and u.uid = user_id()
  15724.     and ( suser_id() = 1     /* User is the System Administrator */
  15725.     or (
  15726.         o1.uid = user_id()   /* User created the object */
  15727.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15728.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15729.             from sysprotects p
  15730.             where p.id = o1.id
  15731.             /* get rows for public,current user,user's group */
  15732.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15733.             /* check for SELECT,EXECUTE privilege */
  15734.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15735.             ) = 1   /* final magic...compare Grants   */
  15736.         )
  15737.     and (
  15738.         o2.uid = user_id()   /* User created the object */
  15739.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15740.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15741.             from sysprotects p
  15742.             where p.id = o2.id
  15743.             /* get rows for public,current user,user's group */
  15744.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15745.             /* check for SELECT,EXECUTE privilege */
  15746.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15747.             ) = 1   /* final magic...compare Grants   */
  15748.         )
  15749.     )
  15750.     union all
  15751.     select
  15752.         db_name(r.rkeydbid),
  15753.         user_name(o1.uid),
  15754.         o1.name,
  15755.         c1.name,
  15756.         convert(binary(16),null),
  15757.         convert(int,null),
  15758.         db_name(r.fkeydbid),
  15759.         user_name(o2.uid),
  15760.         o2.name,
  15761.         c2.name,
  15762.         convert(binary(16),null),
  15763.         convert(int,null),
  15764.         convert(int,14),
  15765.         'NO ACTION',
  15766.         'NO ACTION',
  15767.         i.name,
  15768.         object_name(r.constid),
  15769.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15770.     from
  15771.         sysobjects o1, sysobjects o2,
  15772.         syscolumns c1, syscolumns c2,
  15773.         sysreferences r, sysindexes i, sysusers u
  15774.     where   
  15775.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15776.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15777.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15778.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15779.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15780.     and o1.id = r.rkeyid
  15781.     and r.keycnt >= 14
  15782.     and o1.id = c1.id
  15783.     and c1.colid = r.rkey14
  15784.     and r.fkeyid = o2.id
  15785.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15786.     and o2.id = c2.id   
  15787.     and c2.colid = r.fkey14
  15788.     and i.id = r.rkeyid
  15789.     and i.indid = r.rkeyindid
  15790.     and u.uid = user_id()
  15791.     and ( suser_id() = 1     /* User is the System Administrator */
  15792.     or (
  15793.         o1.uid = user_id()   /* User created the object */
  15794.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15795.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15796.             from sysprotects p
  15797.             where p.id = o1.id
  15798.             /* get rows for public,current user,user's group */
  15799.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15800.             /* check for SELECT,EXECUTE privilege */
  15801.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15802.             ) = 1   /* final magic...compare Grants   */
  15803.         )
  15804.     and (
  15805.         o2.uid = user_id()   /* User created the object */
  15806.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15807.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15808.             from sysprotects p
  15809.             where p.id = o2.id
  15810.             /* get rows for public,current user,user's group */
  15811.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15812.             /* check for SELECT,EXECUTE privilege */
  15813.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15814.             ) = 1   /* final magic...compare Grants   */
  15815.         )
  15816.     )
  15817.     union all
  15818.     select
  15819.         db_name(r.rkeydbid),
  15820.         user_name(o1.uid),
  15821.         o1.name,
  15822.         c1.name,
  15823.         convert(binary(16),null),
  15824.         convert(int,null),
  15825.         db_name(r.fkeydbid),
  15826.         user_name(o2.uid),
  15827.         o2.name,
  15828.         c2.name,
  15829.         convert(binary(16),null),
  15830.         convert(int,null),
  15831.         convert(int,15),
  15832.         'NO ACTION',
  15833.         'NO ACTION',
  15834.         i.name,
  15835.         object_name(r.constid),
  15836.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15837.     from
  15838.         sysobjects o1, sysobjects o2,
  15839.         syscolumns c1, syscolumns c2,
  15840.         sysreferences r, sysindexes i, sysusers u
  15841.     where   
  15842.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15843.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15844.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15845.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15846.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15847.     and o1.id = r.rkeyid
  15848.     and r.keycnt >= 15
  15849.     and o1.id = c1.id
  15850.     and c1.colid = r.rkey15
  15851.     and r.fkeyid = o2.id
  15852.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15853.     and o2.id = c2.id   
  15854.     and c2.colid = r.fkey15
  15855.     and i.id = r.rkeyid
  15856.     and i.indid = r.rkeyindid
  15857.     and u.uid = user_id()
  15858.     and ( suser_id() = 1     /* User is the System Administrator */
  15859.     or (
  15860.         o1.uid = user_id()   /* User created the object */
  15861.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15862.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15863.             from sysprotects p
  15864.             where p.id = o1.id
  15865.             /* get rows for public,current user,user's group */
  15866.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15867.             /* check for SELECT,EXECUTE privilege */
  15868.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15869.             ) = 1   /* final magic...compare Grants   */
  15870.         )
  15871.     and (
  15872.         o2.uid = user_id()   /* User created the object */
  15873.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15874.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15875.             from sysprotects p
  15876.             where p.id = o2.id
  15877.             /* get rows for public,current user,user's group */
  15878.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15879.             /* check for SELECT,EXECUTE privilege */
  15880.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15881.             ) = 1   /* final magic...compare Grants   */
  15882.         )
  15883.     )
  15884.     union all
  15885.     select
  15886.         db_name(r.rkeydbid),
  15887.         user_name(o1.uid),
  15888.         o1.name,
  15889.         c1.name,
  15890.         convert(binary(16),null),
  15891.         convert(int,null),
  15892.         db_name(r.fkeydbid),
  15893.         user_name(o2.uid),
  15894.         o2.name,
  15895.         c2.name,
  15896.         convert(binary(16),null),
  15897.         convert(int,null),
  15898.         convert(int,16),
  15899.         'NO ACTION',
  15900.         'NO ACTION',
  15901.         i.name,
  15902.         object_name(r.constid),
  15903.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15904.     from
  15905.         sysobjects o1, sysobjects o2,
  15906.         syscolumns c1, syscolumns c2,
  15907.         sysreferences r, sysindexes i, sysusers u
  15908.     where   
  15909.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15910.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15911.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15912.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15913.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15914.     and o1.id = r.rkeyid
  15915.     and o1.id = c1.id
  15916.     and r.keycnt >= 16
  15917.     and c1.colid = r.rkey16
  15918.     and r.fkeyid = o2.id
  15919.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15920.     and o2.id = c2.id   
  15921.     and c2.colid = r.fkey16
  15922.     and i.id = r.rkeyid
  15923.     and i.indid = r.rkeyindid
  15924.     and u.uid = user_id()
  15925.     and ( suser_id() = 1     /* User is the System Administrator */
  15926.     or (
  15927.         o1.uid = user_id()   /* User created the object */
  15928.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15929.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15930.             from sysprotects p
  15931.             where p.id = o1.id
  15932.             /* get rows for public,current user,user's group */
  15933.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15934.             /* check for SELECT,EXECUTE privilege */
  15935.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15936.             ) = 1   /* final magic...compare Grants   */
  15937.         )
  15938.     and (
  15939.         o2.uid = user_id()   /* User created the object */
  15940.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15941.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15942.             from sysprotects p
  15943.             where p.id = o2.id
  15944.             /* get rows for public,current user,user's group */
  15945.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15946.             /* check for SELECT,EXECUTE privilege */
  15947.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15948.             ) = 1   /* final magic...compare Grants   */
  15949.         )
  15950.     )
  15951.     order by 8,9,2,3,13
  15952.     END
  15953. go
  15954. dump tran master with no_log
  15955. go
  15956. CREATE PROCEDURE sp_foreign_keys_rowset;2
  15957.     (
  15958.     @handle         int output,
  15959.     @scrollopt      int output,
  15960.     @ccopt          int output,
  15961.     @rows           int output,
  15962.     @pk_table_name      varchar(255) = null,
  15963.     @pk_table_schema    varchar(255) = null,
  15964.     @pk_table_catalog   varchar(255) = null,
  15965.     @fk_table_name      varchar(255) = null,
  15966.     @fk_table_schema    varchar(255) = null,
  15967.     @fk_table_catalog   varchar(255) = null
  15968.     )
  15969. as
  15970.     declare @ret int
  15971.     
  15972.     SET NOCOUNT ON
  15973.  
  15974.     create table #spfkeysrowset1
  15975.         (
  15976.         PK_TABLE_CATALOG    sysname not null,
  15977.         PK_TABLE_SCHEMA     sysname not null,
  15978.         PK_TABLE_NAME       sysname not null,
  15979.         PK_COLUMN_NAME      sysname not null,
  15980.         PK_COLUMN_GUID      binary(16) null,
  15981.         PK_COLUMN_PROPID    int null,
  15982.         FK_TABLE_CATALOG    sysname not null,
  15983.         FK_TABLE_SCHEMA     sysname not null,
  15984.         FK_TABLE_NAME       sysname not null,
  15985.         FK_COLUMN_NAME      sysname not null,
  15986.         FK_COLUMN_GUID      binary(16) null,
  15987.         FK_COLUMN_PROPID    int null,
  15988.         ORDINAL             int not null,
  15989.         UPDATE_RULE         sysname not null,
  15990.         DELETE_RULE         sysname not null,
  15991.         PK_NAME             sysname not null,
  15992.         FK_NAME             sysname not null,
  15993.         DEFERRABILITY       smallint not null       
  15994.         )
  15995.     BEGIN
  15996.     insert into #spfkeysrowset1
  15997.     select
  15998.         db_name(r.rkeydbid),
  15999.         user_name(o1.uid),
  16000.         o1.name,
  16001.         c1.name,
  16002.         convert(binary(16),null),
  16003.         convert(int,null),
  16004.         db_name(r.fkeydbid),
  16005.         user_name(o2.uid),
  16006.         o2.name,
  16007.         c2.name,
  16008.         convert(binary(16),null),
  16009.         convert(int,null),
  16010.         convert(int,1),
  16011.         'NO ACTION',
  16012.         'NO ACTION',
  16013.         i.name,
  16014.         object_name(r.constid),
  16015.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16016.     from
  16017.         sysobjects o1, sysobjects o2,
  16018.         syscolumns c1, syscolumns c2,
  16019.         sysreferences r, sysindexes i, sysusers u
  16020.     where   
  16021.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16022.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16023.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16024.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16025.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16026.     and o1.id = r.rkeyid
  16027.     and o1.id = c1.id
  16028.     and c1.colid = r.rkey1
  16029.     and r.fkeyid = o2.id
  16030.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16031.     and o2.id = c2.id   
  16032.     and c2.colid = r.fkey1
  16033.     and i.id = r.rkeyid
  16034.     and i.indid = r.rkeyindid
  16035.     and u.uid = user_id()
  16036.     and ( suser_id() = 1     /* User is the System Administrator */
  16037.     or (
  16038.         o1.uid = user_id()   /* User created the object */
  16039.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16040.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16041.             from sysprotects p
  16042.             where p.id = o1.id
  16043.             /* get rows for public,current user,user's group */
  16044.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16045.             /* check for SELECT,EXECUTE privilege */
  16046.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16047.             ) = 1   /* final magic...compare Grants   */
  16048.         )
  16049.     and (
  16050.         o2.uid = user_id()   /* User created the object */
  16051.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16052.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16053.             from sysprotects p
  16054.             where p.id = o2.id
  16055.             /* get rows for public,current user,user's group */
  16056.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16057.             /* check for SELECT,EXECUTE privilege */
  16058.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16059.             ) = 1   /* final magic...compare Grants   */
  16060.         )
  16061.     )   
  16062.     union all
  16063.     select
  16064.         db_name(r.rkeydbid),
  16065.         user_name(o1.uid),
  16066.         o1.name,
  16067.         c1.name,
  16068.         convert(binary(16),null),
  16069.         convert(int,null),
  16070.         db_name(r.fkeydbid),
  16071.         user_name(o2.uid),
  16072.         o2.name,
  16073.         c2.name,
  16074.         convert(binary(16),null),
  16075.         convert(int,null),
  16076.         convert(int,2),
  16077.         'NO ACTION',
  16078.         'NO ACTION',
  16079.         i.name,
  16080.         object_name(r.constid),
  16081.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16082.     from
  16083.         sysobjects o1, sysobjects o2,
  16084.         syscolumns c1, syscolumns c2,
  16085.         sysreferences r, sysindexes i, sysusers u
  16086.     where   
  16087.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16088.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16089.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16090.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16091.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16092.     and o1.id = r.rkeyid
  16093.     and r.keycnt >= 2
  16094.     and o1.id = c1.id
  16095.     and c1.colid = r.rkey2
  16096.     and r.fkeyid = o2.id
  16097.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16098.     and o2.id = c2.id   
  16099.     and c2.colid = r.fkey2
  16100.     and i.id = r.rkeyid
  16101.     and i.indid = r.rkeyindid
  16102.     and u.uid = user_id()
  16103.     and ( suser_id() = 1     /* User is the System Administrator */
  16104.     or (
  16105.         o1.uid = user_id()   /* User created the object */
  16106.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16107.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16108.             from sysprotects p
  16109.             where p.id = o1.id
  16110.             /* get rows for public,current user,user's group */
  16111.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16112.             /* check for SELECT,EXECUTE privilege */
  16113.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16114.             ) = 1   /* final magic...compare Grants   */
  16115.         )
  16116.     and (
  16117.         o2.uid = user_id()   /* User created the object */
  16118.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16119.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16120.             from sysprotects p
  16121.             where p.id = o2.id
  16122.             /* get rows for public,current user,user's group */
  16123.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16124.             /* check for SELECT,EXECUTE privilege */
  16125.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16126.             ) = 1   /* final magic...compare Grants   */
  16127.         )
  16128.     )   
  16129.     union all
  16130.     select
  16131.         db_name(r.rkeydbid),
  16132.         user_name(o1.uid),
  16133.         o1.name,
  16134.         c1.name,
  16135.         convert(binary(16),null),
  16136.         convert(int,null),
  16137.         db_name(r.fkeydbid),
  16138.         user_name(o2.uid),
  16139.         o2.name,
  16140.         c2.name,
  16141.         convert(binary(16),null),
  16142.         convert(int,null),
  16143.         convert(int,3),
  16144.         'NO ACTION',
  16145.         'NO ACTION',
  16146.         i.name,
  16147.         object_name(r.constid),
  16148.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16149.     from
  16150.         sysobjects o1, sysobjects o2,
  16151.         syscolumns c1, syscolumns c2,
  16152.         sysreferences r, sysindexes i, sysusers u
  16153.     where   
  16154.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16155.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16156.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16157.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16158.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16159.     and o1.id = r.rkeyid
  16160.     and r.keycnt >= 3
  16161.     and o1.id = c1.id
  16162.     and c1.colid = r.rkey3
  16163.     and r.fkeyid = o2.id
  16164.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16165.     and o2.id = c2.id   
  16166.     and c2.colid = r.fkey3
  16167.     and i.id = r.rkeyid
  16168.     and i.indid = r.rkeyindid
  16169.     and u.uid = user_id()
  16170.     and ( suser_id() = 1     /* User is the System Administrator */
  16171.     or (
  16172.         o1.uid = user_id()   /* User created the object */
  16173.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16174.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16175.             from sysprotects p
  16176.             where p.id = o1.id
  16177.             /* get rows for public,current user,user's group */
  16178.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16179.             /* check for SELECT,EXECUTE privilege */
  16180.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16181.             ) = 1   /* final magic...compare Grants   */
  16182.         )
  16183.     and (
  16184.         o2.uid = user_id()   /* User created the object */
  16185.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16186.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16187.             from sysprotects p
  16188.             where p.id = o2.id
  16189.             /* get rows for public,current user,user's group */
  16190.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16191.             /* check for SELECT,EXECUTE privilege */
  16192.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16193.             ) = 1   /* final magic...compare Grants   */
  16194.         )
  16195.     )   
  16196.     union all
  16197.     select
  16198.         db_name(r.rkeydbid),
  16199.         user_name(o1.uid),
  16200.         o1.name,
  16201.         c1.name,
  16202.         convert(binary(16),null),
  16203.         convert(int,null),
  16204.         db_name(r.fkeydbid),
  16205.         user_name(o2.uid),
  16206.         o2.name,
  16207.         c2.name,
  16208.         convert(binary(16),null),
  16209.         convert(int,null),
  16210.         convert(int,4),
  16211.         'NO ACTION',
  16212.         'NO ACTION',
  16213.         i.name,
  16214.         object_name(r.constid),
  16215.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16216.     from
  16217.         sysobjects o1, sysobjects o2,
  16218.         syscolumns c1, syscolumns c2,
  16219.         sysreferences r, sysindexes i, sysusers u
  16220.     where   
  16221.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16222.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16223.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16224.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16225.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16226.     and o1.id = r.rkeyid
  16227.     and r.keycnt >= 4
  16228.     and o1.id = c1.id
  16229.     and c1.colid = r.rkey4
  16230.     and r.fkeyid = o2.id
  16231.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16232.     and o2.id = c2.id   
  16233.     and c2.colid = r.fkey4
  16234.     and i.id = r.rkeyid
  16235.     and i.indid = r.rkeyindid
  16236.     and u.uid = user_id()
  16237.     and ( suser_id() = 1     /* User is the System Administrator */
  16238.     or (
  16239.         o1.uid = user_id()   /* User created the object */
  16240.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16241.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16242.             from sysprotects p
  16243.             where p.id = o1.id
  16244.             /* get rows for public,current user,user's group */
  16245.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16246.             /* check for SELECT,EXECUTE privilege */
  16247.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16248.             ) = 1   /* final magic...compare Grants   */
  16249.         )
  16250.     and (
  16251.         o2.uid = user_id()   /* User created the object */
  16252.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16253.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16254.             from sysprotects p
  16255.             where p.id = o2.id
  16256.             /* get rows for public,current user,user's group */
  16257.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16258.             /* check for SELECT,EXECUTE privilege */
  16259.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16260.             ) = 1   /* final magic...compare Grants   */
  16261.         )
  16262.     )   
  16263.     union all
  16264.     select
  16265.         db_name(r.rkeydbid),
  16266.         user_name(o1.uid),
  16267.         o1.name,
  16268.         c1.name,
  16269.         convert(binary(16),null),
  16270.         convert(int,null),
  16271.         db_name(r.fkeydbid),
  16272.         user_name(o2.uid),
  16273.         o2.name,
  16274.         c2.name,
  16275.         convert(binary(16),null),
  16276.         convert(int,null),
  16277.         convert(int,5),
  16278.         'NO ACTION',
  16279.         'NO ACTION',
  16280.         i.name,
  16281.         object_name(r.constid),
  16282.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16283.     from
  16284.         sysobjects o1, sysobjects o2,
  16285.         syscolumns c1, syscolumns c2,
  16286.         sysreferences r, sysindexes i, sysusers u
  16287.     where   
  16288.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16289.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16290.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16291.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16292.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16293.     and o1.id = r.rkeyid
  16294.     and r.keycnt >= 5
  16295.     and o1.id = c1.id
  16296.     and c1.colid = r.rkey5
  16297.     and r.fkeyid = o2.id
  16298.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16299.     and o2.id = c2.id   
  16300.     and c2.colid = r.fkey5
  16301.     and i.id = r.rkeyid
  16302.     and i.indid = r.rkeyindid
  16303.     and u.uid = user_id()
  16304.     and ( suser_id() = 1     /* User is the System Administrator */
  16305.     or (
  16306.         o1.uid = user_id()   /* User created the object */
  16307.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16308.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16309.             from sysprotects p
  16310.             where p.id = o1.id
  16311.             /* get rows for public,current user,user's group */
  16312.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16313.             /* check for SELECT,EXECUTE privilege */
  16314.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16315.             ) = 1   /* final magic...compare Grants   */
  16316.         )
  16317.     and (
  16318.         o2.uid = user_id()   /* User created the object */
  16319.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16320.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16321.             from sysprotects p
  16322.             where p.id = o2.id
  16323.             /* get rows for public,current user,user's group */
  16324.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16325.             /* check for SELECT,EXECUTE privilege */
  16326.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16327.             ) = 1   /* final magic...compare Grants   */
  16328.         )
  16329.     )   
  16330.     union all
  16331.     select
  16332.         db_name(r.rkeydbid),
  16333.         user_name(o1.uid),
  16334.         o1.name,
  16335.         c1.name,
  16336.         convert(binary(16),null),
  16337.         convert(int,null),
  16338.         db_name(r.fkeydbid),
  16339.         user_name(o2.uid),
  16340.         o2.name,
  16341.         c2.name,
  16342.         convert(binary(16),null),
  16343.         convert(int,null),
  16344.         convert(int,6),
  16345.         'NO ACTION',
  16346.         'NO ACTION',
  16347.         i.name,
  16348.         object_name(r.constid),
  16349.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16350.     from
  16351.         sysobjects o1, sysobjects o2,
  16352.         syscolumns c1, syscolumns c2,
  16353.         sysreferences r, sysindexes i, sysusers u
  16354.     where   
  16355.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16356.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16357.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16358.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16359.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16360.     and o1.id = r.rkeyid
  16361.     and r.keycnt >= 6
  16362.     and o1.id = c1.id
  16363.     and c1.colid = r.rkey6
  16364.     and r.fkeyid = o2.id
  16365.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16366.     and o2.id = c2.id   
  16367.     and c2.colid = r.fkey6
  16368.     and i.id = r.rkeyid
  16369.     and i.indid = r.rkeyindid
  16370.     and u.uid = user_id()
  16371.     and ( suser_id() = 1     /* User is the System Administrator */
  16372.     or (
  16373.         o1.uid = user_id()   /* User created the object */
  16374.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16375.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16376.             from sysprotects p
  16377.             where p.id = o1.id
  16378.             /* get rows for public,current user,user's group */
  16379.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16380.             /* check for SELECT,EXECUTE privilege */
  16381.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16382.             ) = 1   /* final magic...compare Grants   */
  16383.         )
  16384.     and (
  16385.         o2.uid = user_id()   /* User created the object */
  16386.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16387.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16388.             from sysprotects p
  16389.             where p.id = o2.id
  16390.             /* get rows for public,current user,user's group */
  16391.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16392.             /* check for SELECT,EXECUTE privilege */
  16393.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16394.             ) = 1   /* final magic...compare Grants   */
  16395.         )
  16396.     )   
  16397.     union all
  16398.     select
  16399.         db_name(r.rkeydbid),
  16400.         user_name(o1.uid),
  16401.         o1.name,
  16402.         c1.name,
  16403.         convert(binary(16),null),
  16404.         convert(int,null),
  16405.         db_name(r.fkeydbid),
  16406.         user_name(o2.uid),
  16407.         o2.name,
  16408.         c2.name,
  16409.         convert(binary(16),null),
  16410.         convert(int,null),
  16411.         convert(int,7),
  16412.         'NO ACTION',
  16413.         'NO ACTION',
  16414.         i.name,
  16415.         object_name(r.constid),
  16416.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16417.     from
  16418.         sysobjects o1, sysobjects o2,
  16419.         syscolumns c1, syscolumns c2,
  16420.         sysreferences r, sysindexes i, sysusers u
  16421.     where   
  16422.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16423.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16424.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16425.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16426.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16427.     and o1.id = r.rkeyid
  16428.     and r.keycnt >= 7
  16429.     and o1.id = c1.id
  16430.     and c1.colid = r.rkey7
  16431.     and r.fkeyid = o2.id
  16432.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16433.     and o2.id = c2.id   
  16434.     and c2.colid = r.fkey7
  16435.     and i.id = r.rkeyid
  16436.     and i.indid = r.rkeyindid
  16437.     and u.uid = user_id()
  16438.     and ( suser_id() = 1     /* User is the System Administrator */
  16439.     or (
  16440.         o1.uid = user_id()   /* User created the object */
  16441.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16442.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16443.             from sysprotects p
  16444.             where p.id = o1.id
  16445.             /* get rows for public,current user,user's group */
  16446.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16447.             /* check for SELECT,EXECUTE privilege */
  16448.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16449.             ) = 1   /* final magic...compare Grants   */
  16450.         )
  16451.     and (
  16452.         o2.uid = user_id()   /* User created the object */
  16453.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16454.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16455.             from sysprotects p
  16456.             where p.id = o2.id
  16457.             /* get rows for public,current user,user's group */
  16458.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16459.             /* check for SELECT,EXECUTE privilege */
  16460.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16461.             ) = 1   /* final magic...compare Grants   */
  16462.         )
  16463.     )   
  16464.     union all
  16465.     select
  16466.         db_name(r.rkeydbid),
  16467.         user_name(o1.uid),
  16468.         o1.name,
  16469.         c1.name,
  16470.         convert(binary(16),null),
  16471.         convert(int,null),
  16472.         db_name(r.fkeydbid),
  16473.         user_name(o2.uid),
  16474.         o2.name,
  16475.         c2.name,
  16476.         convert(binary(16),null),
  16477.         convert(int,null),
  16478.         convert(int,8),
  16479.         'NO ACTION',
  16480.         'NO ACTION',
  16481.         i.name,
  16482.         object_name(r.constid),
  16483.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16484.     from
  16485.         sysobjects o1, sysobjects o2,
  16486.         syscolumns c1, syscolumns c2,
  16487.         sysreferences r, sysindexes i, sysusers u
  16488.     where   
  16489.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16490.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16491.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16492.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16493.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16494.     and o1.id = r.rkeyid
  16495.     and r.keycnt >= 8
  16496.     and o1.id = c1.id
  16497.     and c1.colid = r.rkey8
  16498.     and r.fkeyid = o2.id
  16499.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16500.     and o2.id = c2.id   
  16501.     and c2.colid = r.fkey8
  16502.     and i.id = r.rkeyid
  16503.     and i.indid = r.rkeyindid
  16504.     and u.uid = user_id()
  16505.     and ( suser_id() = 1     /* User is the System Administrator */
  16506.     or (
  16507.         o1.uid = user_id()   /* User created the object */
  16508.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16509.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16510.             from sysprotects p
  16511.             where p.id = o1.id
  16512.             /* get rows for public,current user,user's group */
  16513.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16514.             /* check for SELECT,EXECUTE privilege */
  16515.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16516.             ) = 1   /* final magic...compare Grants   */
  16517.         )
  16518.     and (
  16519.         o2.uid = user_id()   /* User created the object */
  16520.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16521.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16522.             from sysprotects p
  16523.             where p.id = o2.id
  16524.             /* get rows for public,current user,user's group */
  16525.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16526.             /* check for SELECT,EXECUTE privilege */
  16527.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16528.             ) = 1   /* final magic...compare Grants   */
  16529.         )
  16530.     )   
  16531.     union all
  16532.     select
  16533.         db_name(r.rkeydbid),
  16534.         user_name(o1.uid),
  16535.         o1.name,
  16536.         c1.name,
  16537.         convert(binary(16),null),
  16538.         convert(int,null),
  16539.         db_name(r.fkeydbid),
  16540.         user_name(o2.uid),
  16541.         o2.name,
  16542.         c2.name,
  16543.         convert(binary(16),null),
  16544.         convert(int,null),
  16545.         convert(int,9),
  16546.         'NO ACTION',
  16547.         'NO ACTION',
  16548.         i.name,
  16549.         object_name(r.constid),
  16550.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16551.     from
  16552.         sysobjects o1, sysobjects o2,
  16553.         syscolumns c1, syscolumns c2,
  16554.         sysreferences r, sysindexes i, sysusers u
  16555.     where   
  16556.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16557.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16558.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16559.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16560.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16561.     and o1.id = r.rkeyid
  16562.     and r.keycnt >= 9
  16563.     and o1.id = c1.id
  16564.     and c1.colid = r.rkey9
  16565.     and r.fkeyid = o2.id
  16566.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16567.     and o2.id = c2.id   
  16568.     and c2.colid = r.fkey9
  16569.     and i.id = r.rkeyid
  16570.     and i.indid = r.rkeyindid
  16571.     and u.uid = user_id()
  16572.     and ( suser_id() = 1     /* User is the System Administrator */
  16573.     or (
  16574.         o1.uid = user_id()   /* User created the object */
  16575.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16576.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16577.             from sysprotects p
  16578.             where p.id = o1.id
  16579.             /* get rows for public,current user,user's group */
  16580.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16581.             /* check for SELECT,EXECUTE privilege */
  16582.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16583.             ) = 1   /* final magic...compare Grants   */
  16584.         )
  16585.     and (
  16586.         o2.uid = user_id()   /* User created the object */
  16587.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16588.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16589.             from sysprotects p
  16590.             where p.id = o2.id
  16591.             /* get rows for public,current user,user's group */
  16592.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16593.             /* check for SELECT,EXECUTE privilege */
  16594.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16595.             ) = 1   /* final magic...compare Grants   */
  16596.         )
  16597.     )   
  16598.     union all
  16599.     select
  16600.         db_name(r.rkeydbid),
  16601.         user_name(o1.uid),
  16602.         o1.name,
  16603.         c1.name,
  16604.         convert(binary(16),null),
  16605.         convert(int,null),
  16606.         db_name(r.fkeydbid),
  16607.         user_name(o2.uid),
  16608.         o2.name,
  16609.         c2.name,
  16610.         convert(binary(16),null),
  16611.         convert(int,null),
  16612.         convert(int,10),
  16613.         'NO ACTION',
  16614.         'NO ACTION',
  16615.         i.name,
  16616.         object_name(r.constid),
  16617.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16618.     from
  16619.         sysobjects o1, sysobjects o2,
  16620.         syscolumns c1, syscolumns c2,
  16621.         sysreferences r, sysindexes i, sysusers u
  16622.     where   
  16623.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16624.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16625.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16626.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16627.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16628.     and o1.id = r.rkeyid
  16629.     and r.keycnt >= 10
  16630.     and o1.id = c1.id
  16631.     and c1.colid = r.rkey10
  16632.     and r.fkeyid = o2.id
  16633.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16634.     and o2.id = c2.id   
  16635.     and c2.colid = r.fkey10
  16636.     and i.id = r.rkeyid
  16637.     and i.indid = r.rkeyindid
  16638.     and u.uid = user_id()
  16639.     and ( suser_id() = 1     /* User is the System Administrator */
  16640.     or (
  16641.         o1.uid = user_id()   /* User created the object */
  16642.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16643.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16644.             from sysprotects p
  16645.             where p.id = o1.id
  16646.             /* get rows for public,current user,user's group */
  16647.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16648.             /* check for SELECT,EXECUTE privilege */
  16649.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16650.             ) = 1   /* final magic...compare Grants   */
  16651.         )
  16652.     and (
  16653.         o2.uid = user_id()   /* User created the object */
  16654.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16655.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16656.             from sysprotects p
  16657.             where p.id = o2.id
  16658.             /* get rows for public,current user,user's group */
  16659.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16660.             /* check for SELECT,EXECUTE privilege */
  16661.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16662.             ) = 1   /* final magic...compare Grants   */
  16663.         )
  16664.     )   
  16665.     union all
  16666.     select
  16667.         db_name(r.rkeydbid),
  16668.         user_name(o1.uid),
  16669.         o1.name,
  16670.         c1.name,
  16671.         convert(binary(16),null),
  16672.         convert(int,null),
  16673.         db_name(r.fkeydbid),
  16674.         user_name(o2.uid),
  16675.         o2.name,
  16676.         c2.name,
  16677.         convert(binary(16),null),
  16678.         convert(int,null),
  16679.         convert(int,11),
  16680.         'NO ACTION',
  16681.         'NO ACTION',
  16682.         i.name,
  16683.         object_name(r.constid),
  16684.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16685.     from
  16686.         sysobjects o1, sysobjects o2,
  16687.         syscolumns c1, syscolumns c2,
  16688.         sysreferences r, sysindexes i, sysusers u
  16689.     where   
  16690.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16691.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16692.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16693.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16694.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16695.     and o1.id = r.rkeyid
  16696.     and r.keycnt >= 11
  16697.     and o1.id = c1.id
  16698.     and c1.colid = r.rkey11
  16699.     and r.fkeyid = o2.id
  16700.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16701.     and o2.id = c2.id   
  16702.     and c2.colid = r.fkey11
  16703.     and i.id = r.rkeyid
  16704.     and i.indid = r.rkeyindid
  16705.     and u.uid = user_id()
  16706.     and ( suser_id() = 1     /* User is the System Administrator */
  16707.     or (
  16708.         o1.uid = user_id()   /* User created the object */
  16709.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16710.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16711.             from sysprotects p
  16712.             where p.id = o1.id
  16713.             /* get rows for public,current user,user's group */
  16714.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16715.             /* check for SELECT,EXECUTE privilege */
  16716.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16717.             ) = 1   /* final magic...compare Grants   */
  16718.         )
  16719.     and (
  16720.         o2.uid = user_id()   /* User created the object */
  16721.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16722.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16723.             from sysprotects p
  16724.             where p.id = o2.id
  16725.             /* get rows for public,current user,user's group */
  16726.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16727.             /* check for SELECT,EXECUTE privilege */
  16728.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16729.             ) = 1   /* final magic...compare Grants   */
  16730.         )
  16731.     )   
  16732.     union all
  16733.     select
  16734.         db_name(r.rkeydbid),
  16735.         user_name(o1.uid),
  16736.         o1.name,
  16737.         c1.name,
  16738.         convert(binary(16),null),
  16739.         convert(int,null),
  16740.         db_name(r.fkeydbid),
  16741.         user_name(o2.uid),
  16742.         o2.name,
  16743.         c2.name,
  16744.         convert(binary(16),null),
  16745.         convert(int,null),
  16746.         convert(int,12),
  16747.         'NO ACTION',
  16748.         'NO ACTION',
  16749.         i.name,
  16750.         object_name(r.constid),
  16751.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16752.     from
  16753.         sysobjects o1, sysobjects o2,
  16754.         syscolumns c1, syscolumns c2,
  16755.         sysreferences r, sysindexes i, sysusers u
  16756.     where   
  16757.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16758.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16759.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16760.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16761.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16762.     and o1.id = r.rkeyid
  16763.     and r.keycnt >= 12
  16764.     and o1.id = c1.id
  16765.     and c1.colid = r.rkey12
  16766.     and r.fkeyid = o2.id
  16767.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16768.     and o2.id = c2.id   
  16769.     and c2.colid = r.fkey12
  16770.     and i.id = r.rkeyid
  16771.     and i.indid = r.rkeyindid
  16772.     and u.uid = user_id()
  16773.     and ( suser_id() = 1     /* User is the System Administrator */
  16774.     or (
  16775.         o1.uid = user_id()   /* User created the object */
  16776.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16777.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16778.             from sysprotects p
  16779.             where p.id = o1.id
  16780.             /* get rows for public,current user,user's group */
  16781.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16782.             /* check for SELECT,EXECUTE privilege */
  16783.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16784.             ) = 1   /* final magic...compare Grants   */
  16785.         )
  16786.     and (
  16787.         o2.uid = user_id()   /* User created the object */
  16788.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16789.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16790.             from sysprotects p
  16791.             where p.id = o2.id
  16792.             /* get rows for public,current user,user's group */
  16793.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16794.             /* check for SELECT,EXECUTE privilege */
  16795.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16796.             ) = 1   /* final magic...compare Grants   */
  16797.         )
  16798.     )   
  16799.     union all
  16800.     select
  16801.         db_name(r.rkeydbid),
  16802.         user_name(o1.uid),
  16803.         o1.name,
  16804.         c1.name,
  16805.         convert(binary(16),null),
  16806.         convert(int,null),
  16807.         db_name(r.fkeydbid),
  16808.         user_name(o2.uid),
  16809.         o2.name,
  16810.         c2.name,
  16811.         convert(binary(16),null),
  16812.         convert(int,null),
  16813.         convert(int,13),
  16814.         'NO ACTION',
  16815.         'NO ACTION',
  16816.         i.name,
  16817.         object_name(r.constid),
  16818.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16819.     from
  16820.         sysobjects o1, sysobjects o2,
  16821.         syscolumns c1, syscolumns c2,
  16822.         sysreferences r, sysindexes i, sysusers u
  16823.     where   
  16824.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16825.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16826.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16827.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16828.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16829.     and o1.id = r.rkeyid
  16830.     and r.keycnt >= 13
  16831.     and o1.id = c1.id
  16832.     and c1.colid = r.rkey13
  16833.     and r.fkeyid = o2.id
  16834.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16835.     and o2.id = c2.id   
  16836.     and c2.colid = r.fkey13
  16837.     and i.id = r.rkeyid
  16838.     and i.indid = r.rkeyindid
  16839.     and u.uid = user_id()
  16840.     and ( suser_id() = 1     /* User is the System Administrator */
  16841.     or (
  16842.         o1.uid = user_id()   /* User created the object */
  16843.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16844.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16845.             from sysprotects p
  16846.             where p.id = o1.id
  16847.             /* get rows for public,current user,user's group */
  16848.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16849.             /* check for SELECT,EXECUTE privilege */
  16850.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16851.             ) = 1   /* final magic...compare Grants   */
  16852.         )
  16853.     and (
  16854.         o2.uid = user_id()   /* User created the object */
  16855.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16856.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16857.             from sysprotects p
  16858.             where p.id = o2.id
  16859.             /* get rows for public,current user,user's group */
  16860.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16861.             /* check for SELECT,EXECUTE privilege */
  16862.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16863.             ) = 1   /* final magic...compare Grants   */
  16864.         )
  16865.     )   
  16866.     union all
  16867.     select
  16868.         db_name(r.rkeydbid),
  16869.         user_name(o1.uid),
  16870.         o1.name,
  16871.         c1.name,
  16872.         convert(binary(16),null),
  16873.         convert(int,null),
  16874.         db_name(r.fkeydbid),
  16875.         user_name(o2.uid),
  16876.         o2.name,
  16877.         c2.name,
  16878.         convert(binary(16),null),
  16879.         convert(int,null),
  16880.         convert(int,14),
  16881.         'NO ACTION',
  16882.         'NO ACTION',
  16883.         i.name,
  16884.         object_name(r.constid),
  16885.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16886.     from
  16887.         sysobjects o1, sysobjects o2,
  16888.         syscolumns c1, syscolumns c2,
  16889.         sysreferences r, sysindexes i, sysusers u
  16890.     where   
  16891.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16892.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16893.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16894.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16895.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16896.     and o1.id = r.rkeyid
  16897.     and r.keycnt >= 14
  16898.     and o1.id = c1.id
  16899.     and c1.colid = r.rkey14
  16900.     and r.fkeyid = o2.id
  16901.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16902.     and o2.id = c2.id   
  16903.     and c2.colid = r.fkey14
  16904.     and i.id = r.rkeyid
  16905.     and i.indid = r.rkeyindid
  16906.     and u.uid = user_id()
  16907.     and ( suser_id() = 1     /* User is the System Administrator */
  16908.     or (
  16909.         o1.uid = user_id()   /* User created the object */
  16910.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16911.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16912.             from sysprotects p
  16913.             where p.id = o1.id
  16914.             /* get rows for public,current user,user's group */
  16915.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16916.             /* check for SELECT,EXECUTE privilege */
  16917.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16918.             ) = 1   /* final magic...compare Grants   */
  16919.         )
  16920.     and (
  16921.         o2.uid = user_id()   /* User created the object */
  16922.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16923.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16924.             from sysprotects p
  16925.             where p.id = o2.id
  16926.             /* get rows for public,current user,user's group */
  16927.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16928.             /* check for SELECT,EXECUTE privilege */
  16929.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16930.             ) = 1   /* final magic...compare Grants   */
  16931.         )
  16932.     )   
  16933.     union all
  16934.     select
  16935.         db_name(r.rkeydbid),
  16936.         user_name(o1.uid),
  16937.         o1.name,
  16938.         c1.name,
  16939.         convert(binary(16),null),
  16940.         convert(int,null),
  16941.         db_name(r.fkeydbid),
  16942.         user_name(o2.uid),
  16943.         o2.name,
  16944.         c2.name,
  16945.         convert(binary(16),null),
  16946.         convert(int,null),
  16947.         convert(int,15),
  16948.         'NO ACTION',
  16949.         'NO ACTION',
  16950.         i.name,
  16951.         object_name(r.constid),
  16952.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16953.     from
  16954.         sysobjects o1, sysobjects o2,
  16955.         syscolumns c1, syscolumns c2,
  16956.         sysreferences r, sysindexes i, sysusers u
  16957.     where   
  16958.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16959.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16960.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16961.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16962.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16963.     and o1.id = r.rkeyid
  16964.     and r.keycnt >= 15
  16965.     and o1.id = c1.id
  16966.     and c1.colid = r.rkey15
  16967.     and r.fkeyid = o2.id
  16968.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16969.     and o2.id = c2.id   
  16970.     and c2.colid = r.fkey15
  16971.     and i.id = r.rkeyid
  16972.     and i.indid = r.rkeyindid
  16973.     and u.uid = user_id()
  16974.     and ( suser_id() = 1     /* User is the System Administrator */
  16975.     or (
  16976.         o1.uid = user_id()   /* User created the object */
  16977.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16978.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16979.             from sysprotects p
  16980.             where p.id = o1.id
  16981.             /* get rows for public,current user,user's group */
  16982.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16983.             /* check for SELECT,EXECUTE privilege */
  16984.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16985.             ) = 1   /* final magic...compare Grants   */
  16986.         )
  16987.     and (
  16988.         o2.uid = user_id()   /* User created the object */
  16989.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16990.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16991.             from sysprotects p
  16992.             where p.id = o2.id
  16993.             /* get rows for public,current user,user's group */
  16994.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16995.             /* check for SELECT,EXECUTE privilege */
  16996.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16997.             ) = 1   /* final magic...compare Grants   */
  16998.         )
  16999.     )   
  17000.     union all
  17001.     select
  17002.         db_name(r.rkeydbid),
  17003.         user_name(o1.uid),
  17004.         o1.name,
  17005.         c1.name,
  17006.         convert(binary(16),null),
  17007.         convert(int,null),
  17008.         db_name(r.fkeydbid),
  17009.         user_name(o2.uid),
  17010.         o2.name,
  17011.         c2.name,
  17012.         convert(binary(16),null),
  17013.         convert(int,null),
  17014.         convert(int,16),
  17015.         'NO ACTION',
  17016.         'NO ACTION',
  17017.         i.name,
  17018.         object_name(r.constid),
  17019.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17020.     from
  17021.         sysobjects o1, sysobjects o2,
  17022.         syscolumns c1, syscolumns c2,
  17023.         sysreferences r, sysindexes i, sysusers u
  17024.     where   
  17025.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17026.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17027.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17028.     and (@pk_table_name is null or @pk_table_name = o1.name)
  17029.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17030.     and o1.id = r.rkeyid
  17031.     and o1.id = c1.id
  17032.     and r.keycnt >= 16
  17033.     and c1.colid = r.rkey16
  17034.     and r.fkeyid = o2.id
  17035.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17036.     and o2.id = c2.id   
  17037.     and c2.colid = r.fkey16
  17038.     and i.id = r.rkeyid
  17039.     and i.indid = r.rkeyindid
  17040.     and u.uid = user_id()
  17041.     and ( suser_id() = 1     /* User is the System Administrator */
  17042.     or (
  17043.         o1.uid = user_id()   /* User created the object */
  17044.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17045.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17046.             from sysprotects p
  17047.             where p.id = o1.id
  17048.             /* get rows for public,current user,user's group */
  17049.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17050.             /* check for SELECT,EXECUTE privilege */
  17051.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17052.             ) = 1   /* final magic...compare Grants   */
  17053.         )
  17054.     and (
  17055.         o2.uid = user_id()   /* User created the object */
  17056.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17057.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17058.             from sysprotects p
  17059.             where p.id = o2.id
  17060.             /* get rows for public,current user,user's group */
  17061.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17062.             /* check for SELECT,EXECUTE privilege */
  17063.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17064.             ) = 1   /* final magic...compare Grants   */
  17065.         )
  17066.     )
  17067.     order by 8,9,2,3,13
  17068.     END
  17069.  
  17070.     exec @ret = sp_cursoropen @handle output, 'select * from #spfkeysrowset1',
  17071.         @scrollopt output, @ccopt output, @rows output
  17072.  
  17073.     drop table #spfkeysrowset1
  17074.     return isnull(@ret,0)
  17075. go
  17076. dump tran master with no_log
  17077. go
  17078. CREATE PROCEDURE sp_foreign_keys_rowset;3
  17079. as
  17080.     select
  17081.         PK_TABLE_CATALOG    = convert(sysname,' '),
  17082.         PK_TABLE_SCHEMA     = convert(sysname,' '),
  17083.         PK_TABLE_NAME       = convert(sysname,' '),
  17084.         PK_COLUMN_NAME      = convert(sysname,' '),
  17085.         PK_COLUMN_GUID      = convert(binary(16),null),
  17086.         PK_COLUMN_PROPID    = convert(int,null),
  17087.         FK_TABLE_CATALOG    = convert(sysname,' '),
  17088.         FK_TABLE_SCHEMA     = convert(sysname,' '),
  17089.         FK_TABLE_NAME       = convert(sysname,' '),
  17090.         FK_COLUMN_NAME      = convert(sysname,' '),
  17091.         FK_COLUMN_GUID      = convert(binary(16),null),
  17092.         FK_COLUMN_PROPID    = convert(int,null),
  17093.         ORDINAL             = convert(int,1),
  17094.         UPDATE_RULE         = 'NO ACTION',
  17095.         DELETE_RULE         = 'NO ACTION',
  17096.         PK_NAME             = convert(sysname, ' '),
  17097.         FK_NAME             = convert(sysname, ' '),
  17098.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17099.     where   1=0
  17100. go
  17101. dump tran master with no_log
  17102. go
  17103.  
  17104.  
  17105. if (charindex('7.00', @@version) = 0 and
  17106.     charindex('8.00', @@version) = 0)
  17107. begin
  17108.     print ''
  17109.     print ''
  17110.     print 'Warning:'
  17111.     print 'you are installing the stored procedures '
  17112.     print 'on a pre 8.0 SQL Server.'
  17113.     print 'Ignore the following errors.'
  17114. end
  17115. else
  17116.     drop proc sp_foreign_keys_rowset
  17117. go
  17118.  
  17119.  
  17120. /*  Procedure for 8.0 server */
  17121. CREATE PROCEDURE sp_foreign_keys_rowset
  17122.     (
  17123.     @pk_table_name      sysname,
  17124.     @pk_table_schema    sysname = null,
  17125.     @fk_table_name      sysname = null,
  17126.     @fk_table_schema    sysname = null,
  17127.     @fk_table_catalog   sysname = null
  17128.     )
  17129. as
  17130.     select
  17131.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17132.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17133.         PK_TABLE_NAME       = o1.name,
  17134.         PK_COLUMN_NAME      = c1.name,
  17135.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17136.         PK_COLUMN_PROPID    = convert(int,null),
  17137.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17138.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17139.         FK_TABLE_NAME       = o2.name,
  17140.         FK_COLUMN_NAME      = c2.name,
  17141.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17142.         FK_COLUMN_PROPID    = convert(int,null),
  17143.         ORDINAL             = convert(int,1),
  17144.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17145.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17146.         PK_NAME             = i.name,
  17147.         FK_NAME             = object_name(r.constid),
  17148.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17149.     from
  17150.         sysobjects o1, sysobjects o2,
  17151.         syscolumns c1, syscolumns c2,
  17152.         sysreferences r, sysindexes i
  17153.     where   
  17154.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17155.     and o1.name = @pk_table_name
  17156.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17157.     and o1.id = r.rkeyid
  17158.     and o1.id = c1.id
  17159.     and c1.colid = r.rkey1
  17160.     and r.fkeyid = o2.id
  17161.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17162.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17163.     and o2.id = c2.id   
  17164.     and c2.colid = r.fkey1
  17165.     and permissions(o1.id) <> 0
  17166.     and permissions(o2.id) <> 0
  17167.     and i.id = r.rkeyid
  17168.     and i.indid = r.rkeyindid
  17169.     union all
  17170.     select
  17171.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17172.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17173.         PK_TABLE_NAME       = o1.name,
  17174.         PK_COLUMN_NAME      = c1.name,
  17175.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17176.         PK_COLUMN_PROPID    = convert(int,null),
  17177.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17178.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17179.         FK_TABLE_NAME       = o2.name,
  17180.         FK_COLUMN_NAME      = c2.name,
  17181.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17182.         FK_COLUMN_PROPID    = convert(int,null),
  17183.         ORDINAL             = convert(int,2),
  17184.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17185.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17186.         PK_NAME             = i.name,
  17187.         FK_NAME             = object_name(r.constid),
  17188.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17189.     from
  17190.         sysobjects o1, sysobjects o2,
  17191.         syscolumns c1, syscolumns c2,
  17192.         sysreferences r, sysindexes i
  17193.     where   
  17194.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17195.     and o1.name = @pk_table_name
  17196.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17197.     and o1.id = r.rkeyid
  17198.     and o1.id = c1.id
  17199.     and c1.colid = r.rkey2
  17200.     and r.keycnt >= 2
  17201.     and r.fkeyid = o2.id
  17202.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17203.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17204.     and o2.id = c2.id   
  17205.     and c2.colid = r.fkey2
  17206.     and permissions(o1.id) <> 0
  17207.     and permissions(o2.id) <> 0
  17208.     and i.id = r.rkeyid
  17209.     and i.indid = r.rkeyindid
  17210.     union all
  17211.     select
  17212.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17213.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17214.         PK_TABLE_NAME       = o1.name,
  17215.         PK_COLUMN_NAME      = c1.name,
  17216.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17217.         PK_COLUMN_PROPID    = convert(int,null),
  17218.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17219.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17220.         FK_TABLE_NAME       = o2.name,
  17221.         FK_COLUMN_NAME      = c2.name,
  17222.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17223.         FK_COLUMN_PROPID    = convert(int,null),
  17224.         ORDINAL             = convert(int,3),
  17225.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17226.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17227.         PK_NAME             = i.name,
  17228.         FK_NAME             = object_name(r.constid),
  17229.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17230.     from
  17231.         sysobjects o1, sysobjects o2,
  17232.         syscolumns c1, syscolumns c2,
  17233.         sysreferences r, sysindexes i
  17234.     where   
  17235.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17236.     and o1.name = @pk_table_name
  17237.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17238.     and o1.id = r.rkeyid
  17239.     and o1.id = c1.id
  17240.     and c1.colid = r.rkey3
  17241.     and r.keycnt >= 3
  17242.     and r.fkeyid = o2.id
  17243.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17244.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17245.     and o2.id = c2.id   
  17246.     and c2.colid = r.fkey3
  17247.     and permissions(o1.id) <> 0
  17248.     and permissions(o2.id) <> 0 
  17249.     and i.id = r.rkeyid
  17250.     and i.indid = r.rkeyindid
  17251.     union all
  17252.     select
  17253.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17254.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17255.         PK_TABLE_NAME       = o1.name,
  17256.         PK_COLUMN_NAME      = c1.name,
  17257.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17258.         PK_COLUMN_PROPID    = convert(int,null),
  17259.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17260.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17261.         FK_TABLE_NAME       = o2.name,
  17262.         FK_COLUMN_NAME      = c2.name,
  17263.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17264.         FK_COLUMN_PROPID    = convert(int,null),
  17265.         ORDINAL             = convert(int,4),
  17266.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17267.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17268.         PK_NAME             = i.name,
  17269.         FK_NAME             = object_name(r.constid),
  17270.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17271.     from
  17272.         sysobjects o1, sysobjects o2,
  17273.         syscolumns c1, syscolumns c2,
  17274.         sysreferences r, sysindexes i
  17275.     where   
  17276.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17277.     and o1.name = @pk_table_name
  17278.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17279.     and o1.id = r.rkeyid
  17280.     and r.keycnt >= 4
  17281.     and o1.id = c1.id
  17282.     and c1.colid = r.rkey4
  17283.     and r.fkeyid = o2.id
  17284.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17285.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17286.     and o2.id = c2.id   
  17287.     and c2.colid = r.fkey4
  17288.     and permissions(o1.id) <> 0
  17289.     and permissions(o2.id) <> 0
  17290.     and i.id = r.rkeyid
  17291.     and i.indid = r.rkeyindid   
  17292.     union all
  17293.     select
  17294.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17295.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17296.         PK_TABLE_NAME       = o1.name,
  17297.         PK_COLUMN_NAME      = c1.name,
  17298.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17299.         PK_COLUMN_PROPID    = convert(int,null),
  17300.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17301.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17302.         FK_TABLE_NAME       = o2.name,
  17303.         FK_COLUMN_NAME      = c2.name,
  17304.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17305.         FK_COLUMN_PROPID    = convert(int,null),
  17306.         ORDINAL             = convert(int,5),
  17307.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17308.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17309.         PK_NAME             = i.name,
  17310.         FK_NAME             = object_name(r.constid),
  17311.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17312.     from
  17313.         sysobjects o1, sysobjects o2,
  17314.         syscolumns c1, syscolumns c2,
  17315.         sysreferences r, sysindexes i
  17316.     where   
  17317.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17318.     and o1.name = @pk_table_name
  17319.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17320.     and o1.id = r.rkeyid
  17321.     and r.keycnt >= 5
  17322.     and o1.id = c1.id
  17323.     and c1.colid = r.rkey5
  17324.     and r.fkeyid = o2.id
  17325.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17326.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17327.     and o2.id = c2.id   
  17328.     and c2.colid = r.fkey5
  17329.     and permissions(o1.id) <> 0
  17330.     and permissions(o2.id) <> 0
  17331.     and i.id = r.rkeyid
  17332.     and i.indid = r.rkeyindid       
  17333.     union all
  17334.     select
  17335.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17336.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17337.         PK_TABLE_NAME       = o1.name,
  17338.         PK_COLUMN_NAME      = c1.name,
  17339.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17340.         PK_COLUMN_PROPID    = convert(int,null),
  17341.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17342.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17343.         FK_TABLE_NAME       = o2.name,
  17344.         FK_COLUMN_NAME      = c2.name,
  17345.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17346.         FK_COLUMN_PROPID    = convert(int,null),
  17347.         ORDINAL             = convert(int,6),
  17348.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17349.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17350.         PK_NAME             = i.name,
  17351.         FK_NAME             = object_name(r.constid),
  17352.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17353.     from
  17354.         sysobjects o1, sysobjects o2,
  17355.         syscolumns c1, syscolumns c2,
  17356.         sysreferences r, sysindexes i
  17357.     where   
  17358.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17359.     and o1.name = @pk_table_name
  17360.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17361.     and o1.id = r.rkeyid
  17362.     and r.keycnt >= 6
  17363.     and o1.id = c1.id
  17364.     and c1.colid = r.rkey6
  17365.     and r.fkeyid = o2.id
  17366.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17367.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17368.     and o2.id = c2.id   
  17369.     and c2.colid = r.fkey6
  17370.     and permissions(o1.id) <> 0
  17371.     and permissions(o2.id) <> 0
  17372.     and i.id = r.rkeyid
  17373.     and i.indid = r.rkeyindid           
  17374.     union all
  17375.     select
  17376.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17377.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17378.         PK_TABLE_NAME       = o1.name,
  17379.         PK_COLUMN_NAME      = c1.name,
  17380.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17381.         PK_COLUMN_PROPID    = convert(int,null),
  17382.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17383.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17384.         FK_TABLE_NAME       = o2.name,
  17385.         FK_COLUMN_NAME      = c2.name,
  17386.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17387.         FK_COLUMN_PROPID    = convert(int,null),
  17388.         ORDINAL             = convert(int,7),
  17389.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17390.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17391.         PK_NAME             = i.name,
  17392.         FK_NAME             = object_name(r.constid),
  17393.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17394.     from
  17395.         sysobjects o1, sysobjects o2,
  17396.         syscolumns c1, syscolumns c2,
  17397.         sysreferences r, sysindexes i
  17398.     where   
  17399.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17400.     and o1.name = @pk_table_name
  17401.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17402.     and o1.id = r.rkeyid
  17403.     and r.keycnt >= 7
  17404.     and o1.id = c1.id
  17405.     and c1.colid = r.rkey7
  17406.     and r.fkeyid = o2.id
  17407.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17408.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17409.     and o2.id = c2.id   
  17410.     and c2.colid = r.fkey7
  17411.     and permissions(o1.id) <> 0
  17412.     and permissions(o2.id) <> 0
  17413.     and i.id = r.rkeyid
  17414.     and i.indid = r.rkeyindid           
  17415.     union all
  17416.     select
  17417.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17418.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17419.         PK_TABLE_NAME       = o1.name,
  17420.         PK_COLUMN_NAME      = c1.name,
  17421.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17422.         PK_COLUMN_PROPID    = convert(int,null),
  17423.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17424.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17425.         FK_TABLE_NAME       = o2.name,
  17426.         FK_COLUMN_NAME      = c2.name,
  17427.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17428.         FK_COLUMN_PROPID    = convert(int,null),
  17429.         ORDINAL             = convert(int,8),
  17430.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17431.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17432.         PK_NAME             = i.name,
  17433.         FK_NAME             = object_name(r.constid),
  17434.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17435.     from
  17436.         sysobjects o1, sysobjects o2,
  17437.         syscolumns c1, syscolumns c2,
  17438.         sysreferences r, sysindexes i
  17439.     where   
  17440.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17441.     and o1.name = @pk_table_name
  17442.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17443.     and o1.id = r.rkeyid
  17444.     and r.keycnt >= 8
  17445.     and o1.id = c1.id
  17446.     and c1.colid = r.rkey8
  17447.     and r.fkeyid = o2.id
  17448.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17449.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17450.     and o2.id = c2.id   
  17451.     and c2.colid = r.fkey8
  17452.     and permissions(o1.id) <> 0
  17453.     and permissions(o2.id) <> 0
  17454.     and i.id = r.rkeyid
  17455.     and i.indid = r.rkeyindid           
  17456.     union all
  17457.     select
  17458.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17459.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17460.         PK_TABLE_NAME       = o1.name,
  17461.         PK_COLUMN_NAME      = c1.name,
  17462.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17463.         PK_COLUMN_PROPID    = convert(int,null),
  17464.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17465.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17466.         FK_TABLE_NAME       = o2.name,
  17467.         FK_COLUMN_NAME      = c2.name,
  17468.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17469.         FK_COLUMN_PROPID    = convert(int,null),
  17470.         ORDINAL             = convert(int,9),
  17471.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17472.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17473.         PK_NAME             = i.name,
  17474.         FK_NAME             = object_name(r.constid),
  17475.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17476.     from
  17477.         sysobjects o1, sysobjects o2,
  17478.         syscolumns c1, syscolumns c2,
  17479.         sysreferences r, sysindexes i
  17480.     where   
  17481.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17482.     and o1.name = @pk_table_name
  17483.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17484.     and o1.id = r.rkeyid
  17485.     and r.keycnt >= 9
  17486.     and o1.id = c1.id
  17487.     and c1.colid = r.rkey9
  17488.     and r.fkeyid = o2.id
  17489.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17490.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17491.     and o2.id = c2.id   
  17492.     and c2.colid = r.fkey9
  17493.     and permissions(o1.id) <> 0
  17494.     and permissions(o2.id) <> 0
  17495.     and i.id = r.rkeyid
  17496.     and i.indid = r.rkeyindid           
  17497.     union all
  17498.     select
  17499.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17500.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17501.         PK_TABLE_NAME       = o1.name,
  17502.         PK_COLUMN_NAME      = c1.name,
  17503.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17504.         PK_COLUMN_PROPID    = convert(int,null),
  17505.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17506.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17507.         FK_TABLE_NAME       = o2.name,
  17508.         FK_COLUMN_NAME      = c2.name,
  17509.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17510.         FK_COLUMN_PROPID    = convert(int,null),
  17511.         ORDINAL             = convert(int,10),
  17512.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17513.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17514.         PK_NAME             = i.name,
  17515.         FK_NAME             = object_name(r.constid),
  17516.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17517.     from
  17518.         sysobjects o1, sysobjects o2,
  17519.         syscolumns c1, syscolumns c2,
  17520.         sysreferences r, sysindexes i
  17521.     where   
  17522.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17523.     and o1.name = @pk_table_name
  17524.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17525.     and o1.id = r.rkeyid
  17526.     and r.keycnt >= 10
  17527.     and o1.id = c1.id
  17528.     and c1.colid = r.rkey10
  17529.     and r.fkeyid = o2.id
  17530.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17531.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17532.     and o2.id = c2.id   
  17533.     and c2.colid = r.fkey10
  17534.     and permissions(o1.id) <> 0
  17535.     and permissions(o2.id) <> 0
  17536.     and i.id = r.rkeyid
  17537.     and i.indid = r.rkeyindid           
  17538.     union all
  17539.     select
  17540.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17541.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17542.         PK_TABLE_NAME       = o1.name,
  17543.         PK_COLUMN_NAME      = c1.name,
  17544.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17545.         PK_COLUMN_PROPID    = convert(int,null),
  17546.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17547.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17548.         FK_TABLE_NAME       = o2.name,
  17549.         FK_COLUMN_NAME      = c2.name,
  17550.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17551.         FK_COLUMN_PROPID    = convert(int,null),
  17552.         ORDINAL             = convert(int,11),
  17553.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17554.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17555.         PK_NAME             = i.name,
  17556.         FK_NAME             = object_name(r.constid),
  17557.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17558.     from
  17559.         sysobjects o1, sysobjects o2,
  17560.         syscolumns c1, syscolumns c2,
  17561.         sysreferences r, sysindexes i
  17562.     where   
  17563.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17564.     and o1.name = @pk_table_name
  17565.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17566.     and o1.id = r.rkeyid
  17567.     and r.keycnt >= 11
  17568.     and o1.id = c1.id
  17569.     and c1.colid = r.rkey11
  17570.     and r.fkeyid = o2.id
  17571.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17572.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17573.     and o2.id = c2.id   
  17574.     and c2.colid = r.fkey11
  17575.     and permissions(o1.id) <> 0
  17576.     and permissions(o2.id) <> 0
  17577.     and i.id = r.rkeyid
  17578.     and i.indid = r.rkeyindid           
  17579.     union all
  17580.     select
  17581.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17582.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17583.         PK_TABLE_NAME       = o1.name,
  17584.         PK_COLUMN_NAME      = c1.name,
  17585.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17586.         PK_COLUMN_PROPID    = convert(int,null),
  17587.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17588.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17589.         FK_TABLE_NAME       = o2.name,
  17590.         FK_COLUMN_NAME      = c2.name,
  17591.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17592.         FK_COLUMN_PROPID    = convert(int,null),
  17593.         ORDINAL             = convert(int,12),
  17594.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17595.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17596.         PK_NAME             = i.name,
  17597.         FK_NAME             = object_name(r.constid),
  17598.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17599.     from
  17600.         sysobjects o1, sysobjects o2,
  17601.         syscolumns c1, syscolumns c2,
  17602.         sysreferences r, sysindexes i
  17603.     where   
  17604.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17605.     and o1.name = @pk_table_name
  17606.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17607.     and o1.id = r.rkeyid
  17608.     and r.keycnt >= 12
  17609.     and o1.id = c1.id
  17610.     and c1.colid = r.rkey12
  17611.     and r.fkeyid = o2.id
  17612.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17613.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17614.     and o2.id = c2.id   
  17615.     and c2.colid = r.fkey12
  17616.     and permissions(o1.id) <> 0
  17617.     and permissions(o2.id) <> 0
  17618.     and i.id = r.rkeyid
  17619.     and i.indid = r.rkeyindid           
  17620.     union all
  17621.     select
  17622.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17623.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17624.         PK_TABLE_NAME       = o1.name,
  17625.         PK_COLUMN_NAME      = c1.name,
  17626.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17627.         PK_COLUMN_PROPID    = convert(int,null),
  17628.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17629.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17630.         FK_TABLE_NAME       = o2.name,
  17631.         FK_COLUMN_NAME      = c2.name,
  17632.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17633.         FK_COLUMN_PROPID    = convert(int,null),
  17634.         ORDINAL             = convert(int,13),
  17635.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17636.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17637.         PK_NAME             = i.name,
  17638.         FK_NAME             = object_name(r.constid),
  17639.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17640.     from
  17641.         sysobjects o1, sysobjects o2,
  17642.         syscolumns c1, syscolumns c2,
  17643.         sysreferences r, sysindexes i
  17644.     where   
  17645.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17646.     and o1.name = @pk_table_name
  17647.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17648.     and o1.id = r.rkeyid
  17649.     and r.keycnt >= 13
  17650.     and o1.id = c1.id
  17651.     and c1.colid = r.rkey13
  17652.     and r.fkeyid = o2.id
  17653.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17654.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17655.     and o2.id = c2.id   
  17656.     and c2.colid = r.fkey13
  17657.     and permissions(o1.id) <> 0
  17658.     and permissions(o2.id) <> 0
  17659.     and i.id = r.rkeyid
  17660.     and i.indid = r.rkeyindid           
  17661.     union all
  17662.     select
  17663.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17664.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17665.         PK_TABLE_NAME       = o1.name,
  17666.         PK_COLUMN_NAME      = c1.name,
  17667.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17668.         PK_COLUMN_PROPID    = convert(int,null),
  17669.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17670.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17671.         FK_TABLE_NAME       = o2.name,
  17672.         FK_COLUMN_NAME      = c2.name,
  17673.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17674.         FK_COLUMN_PROPID    = convert(int,null),
  17675.         ORDINAL             = convert(int,14),
  17676.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17677.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17678.         PK_NAME             = i.name,
  17679.         FK_NAME             = object_name(r.constid),
  17680.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17681.     from
  17682.         sysobjects o1, sysobjects o2,
  17683.         syscolumns c1, syscolumns c2,
  17684.         sysreferences r, sysindexes i
  17685.     where   
  17686.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17687.     and o1.name = @pk_table_name
  17688.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17689.     and o1.id = r.rkeyid
  17690.     and r.keycnt >= 14
  17691.     and o1.id = c1.id
  17692.     and c1.colid = r.rkey14
  17693.     and r.fkeyid = o2.id
  17694.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17695.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17696.     and o2.id = c2.id   
  17697.     and c2.colid = r.fkey14
  17698.     and permissions(o1.id) <> 0
  17699.     and permissions(o2.id) <> 0
  17700.     and i.id = r.rkeyid
  17701.     and i.indid = r.rkeyindid           
  17702.     union all
  17703.     select
  17704.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17705.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17706.         PK_TABLE_NAME       = o1.name,
  17707.         PK_COLUMN_NAME      = c1.name,
  17708.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17709.         PK_COLUMN_PROPID    = convert(int,null),
  17710.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17711.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17712.         FK_TABLE_NAME       = o2.name,
  17713.         FK_COLUMN_NAME      = c2.name,
  17714.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17715.         FK_COLUMN_PROPID    = convert(int,null),
  17716.         ORDINAL             = convert(int,15),
  17717.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17718.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17719.         PK_NAME             = i.name,
  17720.         FK_NAME             = object_name(r.constid),
  17721.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17722.     from
  17723.         sysobjects o1, sysobjects o2,
  17724.         syscolumns c1, syscolumns c2,
  17725.         sysreferences r, sysindexes i
  17726.     where   
  17727.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17728.     and o1.name = @pk_table_name
  17729.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17730.     and o1.id = r.rkeyid
  17731.     and r.keycnt >= 15
  17732.     and o1.id = c1.id
  17733.     and c1.colid = r.rkey15
  17734.     and r.fkeyid = o2.id
  17735.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17736.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17737.     and o2.id = c2.id   
  17738.     and c2.colid = r.fkey15
  17739.     and permissions(o1.id) <> 0
  17740.     and permissions(o2.id) <> 0
  17741.     and i.id = r.rkeyid
  17742.     and i.indid = r.rkeyindid           
  17743.     union all
  17744.     select
  17745.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17746.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17747.         PK_TABLE_NAME       = o1.name,
  17748.         PK_COLUMN_NAME      = c1.name,
  17749.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17750.         PK_COLUMN_PROPID    = convert(int,null),
  17751.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17752.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17753.         FK_TABLE_NAME       = o2.name,
  17754.         FK_COLUMN_NAME      = c2.name,
  17755.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17756.         FK_COLUMN_PROPID    = convert(int,null),
  17757.         ORDINAL             = convert(int,16),
  17758.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17759.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17760.         PK_NAME             = i.name,
  17761.         FK_NAME             = object_name(r.constid),
  17762.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17763.     from
  17764.         sysobjects o1, sysobjects o2,
  17765.         syscolumns c1, syscolumns c2,
  17766.         sysreferences r, sysindexes i
  17767.     where   
  17768.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17769.     and o1.name = @pk_table_name
  17770.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17771.     and o1.id = r.rkeyid
  17772.     and r.keycnt >= 16
  17773.     and o1.id = c1.id
  17774.     and c1.colid = r.rkey16
  17775.     and r.fkeyid = o2.id
  17776.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17777.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17778.     and o2.id = c2.id   
  17779.     and c2.colid = r.fkey16
  17780.     and permissions(o1.id) <> 0
  17781.     and permissions(o2.id) <> 0
  17782.     and i.id = r.rkeyid
  17783.     and i.indid = r.rkeyindid       
  17784.     order by 8,9,2,3,13
  17785. go
  17786. dump tran master with no_log
  17787. go
  17788. CREATE PROCEDURE sp_foreign_keys_rowset;2
  17789.     (
  17790.     @fk_table_name      sysname,
  17791.     @fk_table_schema    sysname = null,
  17792.     @pk_table_name      sysname = null,
  17793.     @pk_table_schema    sysname = null,
  17794.     @pk_table_catalog   sysname = null
  17795.     )
  17796. as
  17797.     select
  17798.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17799.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17800.         PK_TABLE_NAME       = o1.name,
  17801.         PK_COLUMN_NAME      = c1.name,
  17802.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17803.         PK_COLUMN_PROPID    = convert(int,null),
  17804.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17805.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17806.         FK_TABLE_NAME       = o2.name,
  17807.         FK_COLUMN_NAME      = c2.name,
  17808.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17809.         FK_COLUMN_PROPID    = convert(int,null),
  17810.         ORDINAL             = convert(int,1),
  17811.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17812.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17813.         PK_NAME             = i.name,
  17814.         FK_NAME             = object_name(r.constid),
  17815.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17816.     from
  17817.         sysobjects o1, sysobjects o2,
  17818.         syscolumns c1, syscolumns c2,
  17819.         sysreferences r, sysindexes i
  17820.     where   
  17821.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17822.     and o2.name = @fk_table_name
  17823.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17824.     and o2.id = r.fkeyid
  17825.     and o2.id = c2.id
  17826.     and c2.colid = r.fkey1
  17827.     and r.rkeyid = o1.id
  17828.     and (@pk_table_name is null or @pk_table_name = o1.name)
  17829.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17830.     and o1.id = c1.id   
  17831.     and c1.colid = r.rkey1
  17832.     and permissions(o1.id) <> 0
  17833.     and permissions(o2.id) <> 0
  17834.     and i.id = r.rkeyid
  17835.     and i.indid = r.rkeyindid       
  17836.     union   all
  17837.     select
  17838.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17839.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17840.         PK_TABLE_NAME       = o1.name,
  17841.         PK_COLUMN_NAME      = c1.name,
  17842.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17843.         PK_COLUMN_PROPID    = convert(int,null),
  17844.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17845.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17846.         FK_TABLE_NAME       = o2.name,
  17847.         FK_COLUMN_NAME      = c2.name,
  17848.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17849.         FK_COLUMN_PROPID    = convert(int,null),
  17850.         ORDINAL             = convert(int,2),
  17851.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17852.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17853.         PK_NAME             = i.name,
  17854.         FK_NAME             = object_name(r.constid),
  17855.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17856.     from
  17857.         sysobjects o1, sysobjects o2,
  17858.         syscolumns c1, syscolumns c2,
  17859.         sysreferences r, sysindexes i
  17860.     where   
  17861.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17862.     and o2.name = @fk_table_name
  17863.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17864.     and o2.id = r.fkeyid
  17865.     and r.keycnt >= 2
  17866.     and o2.id = c2.id
  17867.     and c2.colid = r.fkey2
  17868.     and r.rkeyid = o1.id
  17869.     and (@pk_table_name is null or @pk_table_name = o1.name)
  17870.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17871.     and o1.id = c1.id   
  17872.     and c1.colid = r.rkey2
  17873.     and permissions(o1.id) <> 0
  17874.     and permissions(o2.id) <> 0
  17875.     and i.id = r.rkeyid
  17876.     and i.indid = r.rkeyindid           
  17877.     union   all
  17878.     select
  17879.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17880.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17881.         PK_TABLE_NAME       = o1.name,
  17882.         PK_COLUMN_NAME      = c1.name,
  17883.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17884.         PK_COLUMN_PROPID    = convert(int,null),
  17885.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17886.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17887.         FK_TABLE_NAME       = o2.name,
  17888.         FK_COLUMN_NAME      = c2.name,
  17889.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17890.         FK_COLUMN_PROPID    = convert(int,null),
  17891.         ORDINAL             = convert(int,3),
  17892.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17893.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17894.         PK_NAME             = i.name,
  17895.         FK_NAME             = object_name(r.constid),
  17896.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17897.     from
  17898.         sysobjects o1, sysobjects o2,
  17899.         syscolumns c1, syscolumns c2,
  17900.         sysreferences r, sysindexes i
  17901.     where   
  17902.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17903.     and o2.name = @fk_table_name
  17904.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17905.     and o2.id = r.fkeyid
  17906.     and r.keycnt >= 3
  17907.     and o2.id = c2.id
  17908.     and c2.colid = r.fkey3
  17909.     and r.rkeyid = o1.id
  17910.     and (@pk_table_name is null or @pk_table_name = o1.name)
  17911.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17912.     and o1.id = c1.id   
  17913.     and c1.colid = r.rkey3
  17914.     and permissions(o1.id) <> 0
  17915.     and permissions(o2.id) <> 0
  17916.     and i.id = r.rkeyid
  17917.     and i.indid = r.rkeyindid           
  17918.     union   all
  17919.     select
  17920.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17921.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17922.         PK_TABLE_NAME       = o1.name,
  17923.         PK_COLUMN_NAME      = c1.name,
  17924.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17925.         PK_COLUMN_PROPID    = convert(int,null),
  17926.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17927.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17928.         FK_TABLE_NAME       = o2.name,
  17929.         FK_COLUMN_NAME      = c2.name,
  17930.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17931.         FK_COLUMN_PROPID    = convert(int,null),
  17932.         ORDINAL             = convert(int,4),
  17933.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17934.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17935.         PK_NAME             = i.name,
  17936.         FK_NAME             = object_name(r.constid),
  17937.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17938.     from
  17939.         sysobjects o1, sysobjects o2,
  17940.         syscolumns c1, syscolumns c2,
  17941.         sysreferences r, sysindexes i
  17942.     where   
  17943.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17944.     and o2.name = @fk_table_name
  17945.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17946.     and o2.id = r.fkeyid
  17947.     and r.keycnt >= 4
  17948.     and o2.id = c2.id
  17949.     and c2.colid = r.fkey4
  17950.     and r.rkeyid = o1.id
  17951.     and (@pk_table_name is null or @pk_table_name = o1.name)
  17952.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17953.     and o1.id = c1.id   
  17954.     and c1.colid = r.rkey4
  17955.     and permissions(o1.id) <> 0
  17956.     and permissions(o2.id) <> 0
  17957.     and i.id = r.rkeyid
  17958.     and i.indid = r.rkeyindid           
  17959.     union   all
  17960.     select
  17961.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17962.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17963.         PK_TABLE_NAME       = o1.name,
  17964.         PK_COLUMN_NAME      = c1.name,
  17965.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17966.         PK_COLUMN_PROPID    = convert(int,null),
  17967.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17968.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17969.         FK_TABLE_NAME       = o2.name,
  17970.         FK_COLUMN_NAME      = c2.name,
  17971.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17972.         FK_COLUMN_PROPID    = convert(int,null),
  17973.         ORDINAL             = convert(int,5),
  17974.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17975.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17976.         PK_NAME             = i.name,
  17977.         FK_NAME             = object_name(r.constid),
  17978.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17979.     from
  17980.         sysobjects o1, sysobjects o2,
  17981.         syscolumns c1, syscolumns c2,
  17982.         sysreferences r, sysindexes i
  17983.     where   
  17984.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17985.     and o2.name = @fk_table_name
  17986.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17987.     and o2.id = r.fkeyid
  17988.     and r.keycnt >= 5
  17989.     and o2.id = c2.id
  17990.     and c2.colid = r.fkey5
  17991.     and r.rkeyid = o1.id
  17992.     and (@pk_table_name is null or @pk_table_name = o1.name)
  17993.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17994.     and o1.id = c1.id   
  17995.     and c1.colid = r.rkey5
  17996.     and permissions(o1.id) <> 0
  17997.     and permissions(o2.id) <> 0
  17998.     and i.id = r.rkeyid
  17999.     and i.indid = r.rkeyindid           
  18000.     union   all
  18001.     select
  18002.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18003.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18004.         PK_TABLE_NAME       = o1.name,
  18005.         PK_COLUMN_NAME      = c1.name,
  18006.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18007.         PK_COLUMN_PROPID    = convert(int,null),
  18008.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18009.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18010.         FK_TABLE_NAME       = o2.name,
  18011.         FK_COLUMN_NAME      = c2.name,
  18012.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18013.         FK_COLUMN_PROPID    = convert(int,null),
  18014.         ORDINAL             = convert(int,6),
  18015.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18016.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18017.         PK_NAME             = i.name,
  18018.         FK_NAME             = object_name(r.constid),
  18019.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18020.     from
  18021.         sysobjects o1, sysobjects o2,
  18022.         syscolumns c1, syscolumns c2,
  18023.         sysreferences r, sysindexes i
  18024.     where   
  18025.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18026.     and o2.name = @fk_table_name
  18027.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18028.     and o2.id = r.fkeyid
  18029.     and r.keycnt >= 6
  18030.     and o2.id = c2.id
  18031.     and c2.colid = r.fkey6
  18032.     and r.rkeyid = o1.id
  18033.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18034.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18035.     and o1.id = c1.id       
  18036.     and c1.colid = r.rkey6
  18037.     and permissions(o1.id) <> 0
  18038.     and permissions(o2.id) <> 0
  18039.     and i.id = r.rkeyid
  18040.     and i.indid = r.rkeyindid       
  18041.     union   all
  18042.     select
  18043.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18044.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18045.         PK_TABLE_NAME       = o1.name,
  18046.         PK_COLUMN_NAME      = c1.name,
  18047.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18048.         PK_COLUMN_PROPID    = convert(int,null),
  18049.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18050.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18051.         FK_TABLE_NAME       = o2.name,
  18052.         FK_COLUMN_NAME      = c2.name,
  18053.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18054.         FK_COLUMN_PROPID    = convert(int,null),
  18055.         ORDINAL             = convert(int,7),
  18056.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18057.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18058.         PK_NAME             = i.name,
  18059.         FK_NAME             = object_name(r.constid),
  18060.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18061.     from
  18062.         sysobjects o1, sysobjects o2,
  18063.         syscolumns c1, syscolumns c2,
  18064.         sysreferences r, sysindexes i
  18065.     where   
  18066.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18067.     and o2.name = @fk_table_name
  18068.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18069.     and o2.id = r.fkeyid
  18070.     and r.keycnt >= 7
  18071.     and o2.id = c2.id
  18072.     and c2.colid = r.fkey7
  18073.     and r.rkeyid = o1.id
  18074.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18075.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18076.     and o1.id = c1.id   
  18077.     and c1.colid = r.rkey7
  18078.     and permissions(o1.id) <> 0
  18079.     and permissions(o2.id) <> 0
  18080.     and i.id = r.rkeyid
  18081.     and i.indid = r.rkeyindid           
  18082.     union   all
  18083.     select
  18084.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18085.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18086.         PK_TABLE_NAME       = o1.name,
  18087.         PK_COLUMN_NAME      = c1.name,
  18088.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18089.         PK_COLUMN_PROPID    = convert(int,null),
  18090.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18091.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18092.         FK_TABLE_NAME       = o2.name,
  18093.         FK_COLUMN_NAME      = c2.name,
  18094.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18095.         FK_COLUMN_PROPID    = convert(int,null),
  18096.         ORDINAL             = convert(int,8),
  18097.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18098.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18099.         PK_NAME             = i.name,
  18100.         FK_NAME             = object_name(r.constid),
  18101.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18102.     from
  18103.         sysobjects o1, sysobjects o2,
  18104.         syscolumns c1, syscolumns c2,
  18105.         sysreferences r, sysindexes i
  18106.     where   
  18107.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18108.     and o2.name = @fk_table_name
  18109.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18110.     and o2.id = r.fkeyid
  18111.     and r.keycnt >= 8
  18112.     and o2.id = c2.id
  18113.     and c2.colid = r.fkey8
  18114.     and r.rkeyid = o1.id
  18115.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18116.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18117.     and o1.id = c1.id   
  18118.     and c1.colid = r.rkey8
  18119.     and permissions(o1.id) <> 0
  18120.     and permissions(o2.id) <> 0
  18121.     and i.id = r.rkeyid
  18122.     and i.indid = r.rkeyindid           
  18123.     union   all
  18124.     select
  18125.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18126.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18127.         PK_TABLE_NAME       = o1.name,
  18128.         PK_COLUMN_NAME      = c1.name,
  18129.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18130.         PK_COLUMN_PROPID    = convert(int,null),
  18131.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18132.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18133.         FK_TABLE_NAME       = o2.name,
  18134.         FK_COLUMN_NAME      = c2.name,
  18135.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18136.         FK_COLUMN_PROPID    = convert(int,null),
  18137.         ORDINAL             = convert(int,9),
  18138.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18139.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18140.         PK_NAME             = i.name,
  18141.         FK_NAME             = object_name(r.constid),
  18142.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18143.     from
  18144.         sysobjects o1, sysobjects o2,
  18145.         syscolumns c1, syscolumns c2,
  18146.         sysreferences r, sysindexes i
  18147.     where   
  18148.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18149.     and o2.name = @fk_table_name
  18150.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18151.     and o2.id = r.fkeyid
  18152.     and r.keycnt >= 9
  18153.     and o2.id = c2.id
  18154.     and c2.colid = r.fkey9
  18155.     and r.rkeyid = o1.id
  18156.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18157.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18158.     and o1.id = c1.id   
  18159.     and c1.colid = r.rkey9
  18160.     and permissions(o1.id) <> 0
  18161.     and permissions(o2.id) <> 0
  18162.     and i.id = r.rkeyid
  18163.     and i.indid = r.rkeyindid           
  18164.     union   all
  18165.     select
  18166.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18167.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18168.         PK_TABLE_NAME       = o1.name,
  18169.         PK_COLUMN_NAME      = c1.name,
  18170.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18171.         PK_COLUMN_PROPID    = convert(int,null),
  18172.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18173.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18174.         FK_TABLE_NAME       = o2.name,
  18175.         FK_COLUMN_NAME      = c2.name,
  18176.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18177.         FK_COLUMN_PROPID    = convert(int,null),
  18178.         ORDINAL             = convert(int,10),
  18179.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18180.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18181.         PK_NAME             = i.name,
  18182.         FK_NAME             = object_name(r.constid),
  18183.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18184.     from
  18185.         sysobjects o1, sysobjects o2,
  18186.         syscolumns c1, syscolumns c2,
  18187.         sysreferences r, sysindexes i
  18188.     where   
  18189.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18190.     and o2.name = @fk_table_name
  18191.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18192.     and o2.id = r.fkeyid
  18193.     and r.keycnt >= 10
  18194.     and o2.id = c2.id
  18195.     and c2.colid = r.fkey10
  18196.     and r.rkeyid = o1.id
  18197.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18198.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18199.     and o1.id = c1.id   
  18200.     and c1.colid = r.rkey10
  18201.     and permissions(o1.id) <> 0
  18202.     and permissions(o2.id) <> 0
  18203.     and i.id = r.rkeyid
  18204.     and i.indid = r.rkeyindid           
  18205.     union   all
  18206.     select
  18207.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18208.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18209.         PK_TABLE_NAME       = o1.name,
  18210.         PK_COLUMN_NAME      = c1.name,
  18211.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18212.         PK_COLUMN_PROPID    = convert(int,null),
  18213.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18214.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18215.         FK_TABLE_NAME       = o2.name,
  18216.         FK_COLUMN_NAME      = c2.name,
  18217.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18218.         FK_COLUMN_PROPID    = convert(int,null),
  18219.         ORDINAL             = convert(int,11),
  18220.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18221.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18222.         PK_NAME             = i.name,
  18223.         FK_NAME             = object_name(r.constid),
  18224.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18225.     from
  18226.         sysobjects o1, sysobjects o2,
  18227.         syscolumns c1, syscolumns c2,
  18228.         sysreferences r, sysindexes i
  18229.     where   
  18230.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18231.     and o2.name = @fk_table_name
  18232.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18233.     and o2.id = r.fkeyid
  18234.     and r.keycnt >= 11
  18235.     and o2.id = c2.id
  18236.     and c2.colid = r.fkey11
  18237.     and r.rkeyid = o1.id
  18238.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18239.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18240.     and o1.id = c1.id   
  18241.     and c1.colid = r.rkey11
  18242.     and permissions(o1.id) <> 0
  18243.     and permissions(o2.id) <> 0
  18244.     and i.id = r.rkeyid
  18245.     and i.indid = r.rkeyindid           
  18246.     union   all
  18247.     select
  18248.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18249.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18250.         PK_TABLE_NAME       = o1.name,
  18251.         PK_COLUMN_NAME      = c1.name,
  18252.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18253.         PK_COLUMN_PROPID    = convert(int,null),
  18254.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18255.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18256.         FK_TABLE_NAME       = o2.name,
  18257.         FK_COLUMN_NAME      = c2.name,
  18258.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18259.         FK_COLUMN_PROPID    = convert(int,null),
  18260.         ORDINAL             = convert(int,12),
  18261.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18262.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18263.         PK_NAME             = i.name,
  18264.         FK_NAME             = object_name(r.constid),
  18265.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18266.     from
  18267.         sysobjects o1, sysobjects o2,
  18268.         syscolumns c1, syscolumns c2,
  18269.         sysreferences r, sysindexes i
  18270.     where   
  18271.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18272.     and o2.name = @fk_table_name
  18273.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18274.     and o2.id = r.fkeyid
  18275.     and r.keycnt >= 12
  18276.     and o2.id = c2.id
  18277.     and c2.colid = r.fkey12
  18278.     and r.rkeyid = o1.id
  18279.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18280.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18281.     and o1.id = c1.id   
  18282.     and c1.colid = r.rkey12
  18283.     and permissions(o1.id) <> 0
  18284.     and permissions(o2.id) <> 0
  18285.     and i.id = r.rkeyid
  18286.     and i.indid = r.rkeyindid           
  18287.     union   all
  18288.     select
  18289.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18290.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18291.         PK_TABLE_NAME       = o1.name,
  18292.         PK_COLUMN_NAME      = c1.name,
  18293.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18294.         PK_COLUMN_PROPID    = convert(int,null),
  18295.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18296.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18297.         FK_TABLE_NAME       = o2.name,
  18298.         FK_COLUMN_NAME      = c2.name,
  18299.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18300.         FK_COLUMN_PROPID    = convert(int,null),
  18301.         ORDINAL             = convert(int,13),
  18302.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18303.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18304.         PK_NAME             = i.name,
  18305.         FK_NAME             = object_name(r.constid),
  18306.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18307.     from
  18308.         sysobjects o1, sysobjects o2,
  18309.         syscolumns c1, syscolumns c2,
  18310.         sysreferences r, sysindexes i
  18311.     where   
  18312.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18313.     and o2.name = @fk_table_name
  18314.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18315.     and o2.id = r.fkeyid
  18316.     and r.keycnt >= 13
  18317.     and o2.id = c2.id
  18318.     and c2.colid = r.fkey13
  18319.     and r.rkeyid = o1.id
  18320.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18321.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18322.     and o1.id = c1.id   
  18323.     and c1.colid = r.rkey13
  18324.     and permissions(o1.id) <> 0
  18325.     and permissions(o2.id) <> 0
  18326.     and i.id = r.rkeyid
  18327.     and i.indid = r.rkeyindid           
  18328.     union   all
  18329.     select
  18330.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18331.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18332.         PK_TABLE_NAME       = o1.name,
  18333.         PK_COLUMN_NAME      = c1.name,
  18334.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18335.         PK_COLUMN_PROPID    = convert(int,null),
  18336.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18337.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18338.         FK_TABLE_NAME       = o2.name,
  18339.         FK_COLUMN_NAME      = c2.name,
  18340.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18341.         FK_COLUMN_PROPID    = convert(int,null),
  18342.         ORDINAL             = convert(int,14),
  18343.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18344.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18345.         PK_NAME             = i.name,
  18346.         FK_NAME             = object_name(r.constid),
  18347.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18348.     from
  18349.         sysobjects o1, sysobjects o2,
  18350.         syscolumns c1, syscolumns c2,
  18351.         sysreferences r, sysindexes i
  18352.     where   
  18353.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18354.     and o2.name = @fk_table_name
  18355.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18356.     and o2.id = r.fkeyid
  18357.     and r.keycnt >= 14
  18358.     and o2.id = c2.id
  18359.     and c2.colid = r.fkey14
  18360.     and r.rkeyid = o1.id
  18361.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18362.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18363.     and o1.id = c1.id   
  18364.     and c1.colid = r.rkey14
  18365.     and permissions(o1.id) <> 0
  18366.     and permissions(o2.id) <> 0
  18367.     and i.id = r.rkeyid
  18368.     and i.indid = r.rkeyindid           
  18369.     union   all
  18370.     select
  18371.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18372.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18373.         PK_TABLE_NAME       = o1.name,
  18374.         PK_COLUMN_NAME      = c1.name,
  18375.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18376.         PK_COLUMN_PROPID    = convert(int,null),
  18377.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18378.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18379.         FK_TABLE_NAME       = o2.name,
  18380.         FK_COLUMN_NAME      = c2.name,
  18381.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18382.         FK_COLUMN_PROPID    = convert(int,null),
  18383.         ORDINAL             = convert(int,15),
  18384.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18385.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18386.         PK_NAME             = i.name,
  18387.         FK_NAME             = object_name(r.constid),
  18388.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/      
  18389.     from
  18390.         sysobjects o1, sysobjects o2,
  18391.         syscolumns c1, syscolumns c2,
  18392.         sysreferences r, sysindexes i
  18393.     where   
  18394.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18395.     and o2.name = @fk_table_name
  18396.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18397.     and o2.id = r.fkeyid
  18398.     and r.keycnt >= 15
  18399.     and o2.id = c2.id
  18400.     and c2.colid = r.fkey15
  18401.     and r.rkeyid = o1.id
  18402.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18403.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18404.     and o1.id = c1.id   
  18405.     and c1.colid = r.rkey15
  18406.     and permissions(o1.id) <> 0
  18407.     and permissions(o2.id) <> 0
  18408.     and i.id = r.rkeyid
  18409.     and i.indid = r.rkeyindid           
  18410.     union   all
  18411.     select
  18412.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18413.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18414.         PK_TABLE_NAME       = o1.name,
  18415.         PK_COLUMN_NAME      = c1.name,
  18416.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18417.         PK_COLUMN_PROPID    = convert(int,null),
  18418.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18419.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18420.         FK_TABLE_NAME       = o2.name,
  18421.         FK_COLUMN_NAME      = c2.name,
  18422.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18423.         FK_COLUMN_PROPID    = convert(int,null),
  18424.         ORDINAL             = convert(int,16),
  18425.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18426.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18427.         PK_NAME             = i.name,
  18428.         FK_NAME             = object_name(r.constid),
  18429.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/      
  18430.     from
  18431.         sysobjects o1, sysobjects o2,
  18432.         syscolumns c1, syscolumns c2,
  18433.         sysreferences r, sysindexes i
  18434.     where   
  18435.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18436.     and o2.name = @fk_table_name
  18437.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18438.     and o2.id = r.fkeyid
  18439.     and r.keycnt >= 16
  18440.     and o2.id = c2.id
  18441.     and c2.colid = r.fkey16
  18442.     and r.rkeyid = o1.id
  18443.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18444.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18445.     and o1.id = c1.id   
  18446.     and c1.colid = r.rkey16
  18447.     and permissions(o1.id) <> 0
  18448.     and permissions(o2.id) <> 0
  18449.     and i.id = r.rkeyid
  18450.     and i.indid = r.rkeyindid       
  18451.     order by 8,9,2,3,13
  18452. go
  18453. dump tran master with no_log
  18454. go
  18455. CREATE PROCEDURE sp_foreign_keys_rowset;3
  18456.     (
  18457.     @pk_table_schema    sysname = null,
  18458.     @pk_table_catalog   sysname = null,
  18459.     @fk_table_schema    sysname = null,
  18460.     @fk_table_catalog   sysname = null
  18461.     )
  18462. as
  18463.     select
  18464.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18465.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18466.         PK_TABLE_NAME       = o1.name,
  18467.         PK_COLUMN_NAME      = c1.name,
  18468.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18469.         PK_COLUMN_PROPID    = convert(int,null),
  18470.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18471.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18472.         FK_TABLE_NAME       = o2.name,
  18473.         FK_COLUMN_NAME      = c2.name,
  18474.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18475.         FK_COLUMN_PROPID    = convert(int,null),
  18476.         ORDINAL             = convert(int,1),
  18477.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18478.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18479.         PK_NAME             = i.name,
  18480.         FK_NAME             = object_name(r.constid),
  18481.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/      
  18482.     from
  18483.         sysobjects o1, sysobjects o2,
  18484.         syscolumns c1, syscolumns c2,
  18485.         sysreferences r, sysindexes i
  18486.     where   
  18487.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18488.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18489.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18490.     and o1.id = r.rkeyid
  18491.     and o1.id = c1.id
  18492.     and c1.colid = r.rkey1
  18493.     and r.fkeyid = o2.id
  18494.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18495.     and o2.id = c2.id   
  18496.     and c2.colid = r.fkey1
  18497.     and permissions(o1.id) <> 0
  18498.     and permissions(o2.id) <> 0
  18499.     and i.id = r.rkeyid
  18500.     and i.indid = r.rkeyindid       
  18501.     union all
  18502.     select
  18503.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18504.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18505.         PK_TABLE_NAME       = o1.name,
  18506.         PK_COLUMN_NAME      = c1.name,
  18507.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18508.         PK_COLUMN_PROPID    = convert(int,null),
  18509.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18510.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18511.         FK_TABLE_NAME       = o2.name,
  18512.         FK_COLUMN_NAME      = c2.name,
  18513.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18514.         FK_COLUMN_PROPID    = convert(int,null),
  18515.         ORDINAL             = convert(int,2),
  18516.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18517.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18518.         PK_NAME             = i.name,
  18519.         FK_NAME             = object_name(r.constid),
  18520.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18521.     from
  18522.         sysobjects o1, sysobjects o2,
  18523.         syscolumns c1, syscolumns c2,
  18524.         sysreferences r, sysindexes i
  18525.     where   
  18526.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18527.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18528.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18529.     and o1.id = r.rkeyid
  18530.     and r.keycnt >= 2
  18531.     and o1.id = c1.id
  18532.     and c1.colid = r.rkey2
  18533.     and r.fkeyid = o2.id
  18534.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18535.     and o2.id = c2.id   
  18536.     and c2.colid = r.fkey2
  18537.     and permissions(o1.id) <> 0
  18538.     and permissions(o2.id) <> 0
  18539.     and i.id = r.rkeyid
  18540.     and i.indid = r.rkeyindid       
  18541.     union all
  18542.     select
  18543.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18544.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18545.         PK_TABLE_NAME       = o1.name,
  18546.         PK_COLUMN_NAME      = c1.name,
  18547.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18548.         PK_COLUMN_PROPID    = convert(int,null),
  18549.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18550.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18551.         FK_TABLE_NAME       = o2.name,
  18552.         FK_COLUMN_NAME      = c2.name,
  18553.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18554.         FK_COLUMN_PROPID    = convert(int,null),
  18555.         ORDINAL             = convert(int,3),
  18556.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18557.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18558.         PK_NAME             = i.name,
  18559.         FK_NAME             = object_name(r.constid),
  18560.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18561.     from
  18562.         sysobjects o1, sysobjects o2,
  18563.         syscolumns c1, syscolumns c2,
  18564.         sysreferences r, sysindexes i
  18565.     where   
  18566.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18567.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18568.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18569.     and o1.id = r.rkeyid
  18570.     and r.keycnt >= 3
  18571.     and o1.id = c1.id
  18572.     and c1.colid = r.rkey3
  18573.     and r.fkeyid = o2.id
  18574.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18575.     and o2.id = c2.id   
  18576.     and c2.colid = r.fkey3
  18577.     and permissions(o1.id) <> 0
  18578.     and permissions(o2.id) <> 0
  18579.     and i.id = r.rkeyid
  18580.     and i.indid = r.rkeyindid       
  18581.     union all
  18582.     select
  18583.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18584.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18585.         PK_TABLE_NAME       = o1.name,
  18586.         PK_COLUMN_NAME      = c1.name,
  18587.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18588.         PK_COLUMN_PROPID    = convert(int,null),
  18589.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18590.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18591.         FK_TABLE_NAME       = o2.name,
  18592.         FK_COLUMN_NAME      = c2.name,
  18593.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18594.         FK_COLUMN_PROPID    = convert(int,null),
  18595.         ORDINAL             = convert(int,4),
  18596.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18597.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18598.         PK_NAME             = i.name,
  18599.         FK_NAME             = object_name(r.constid),
  18600.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18601.     from
  18602.         sysobjects o1, sysobjects o2,
  18603.         syscolumns c1, syscolumns c2,
  18604.         sysreferences r, sysindexes i
  18605.     where   
  18606.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18607.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18608.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18609.     and o1.id = r.rkeyid
  18610.     and r.keycnt >= 4
  18611.     and o1.id = c1.id
  18612.     and c1.colid = r.rkey4
  18613.     and r.fkeyid = o2.id
  18614.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18615.     and o2.id = c2.id   
  18616.     and c2.colid = r.fkey4
  18617.     and permissions(o1.id) <> 0
  18618.     and permissions(o2.id) <> 0
  18619.     and i.id = r.rkeyid
  18620.     and i.indid = r.rkeyindid       
  18621.     union all
  18622.     select
  18623.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18624.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18625.         PK_TABLE_NAME       = o1.name,
  18626.         PK_COLUMN_NAME      = c1.name,
  18627.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18628.         PK_COLUMN_PROPID    = convert(int,null),
  18629.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18630.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18631.         FK_TABLE_NAME       = o2.name,
  18632.         FK_COLUMN_NAME      = c2.name,
  18633.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18634.         FK_COLUMN_PROPID    = convert(int,null),
  18635.         ORDINAL             = convert(int,5),
  18636.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18637.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18638.         PK_NAME             = i.name,
  18639.         FK_NAME             = object_name(r.constid),
  18640.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18641.     from
  18642.         sysobjects o1, sysobjects o2,
  18643.         syscolumns c1, syscolumns c2,
  18644.         sysreferences r, sysindexes i
  18645.     where   
  18646.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18647.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18648.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18649.     and o1.id = r.rkeyid
  18650.     and r.keycnt >= 5
  18651.     and o1.id = c1.id
  18652.     and c1.colid = r.rkey5
  18653.     and r.fkeyid = o2.id
  18654.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18655.     and o2.id = c2.id   
  18656.     and c2.colid = r.fkey5
  18657.     and permissions(o1.id) <> 0
  18658.     and permissions(o2.id) <> 0
  18659.     and i.id = r.rkeyid
  18660.     and i.indid = r.rkeyindid       
  18661.     union all
  18662.     select
  18663.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18664.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18665.         PK_TABLE_NAME       = o1.name,
  18666.         PK_COLUMN_NAME      = c1.name,
  18667.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18668.         PK_COLUMN_PROPID    = convert(int,null),
  18669.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18670.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18671.         FK_TABLE_NAME       = o2.name,
  18672.         FK_COLUMN_NAME      = c2.name,
  18673.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18674.         FK_COLUMN_PROPID    = convert(int,null),
  18675.         ORDINAL             = convert(int,6),
  18676.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18677.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18678.         PK_NAME             = i.name,
  18679.         FK_NAME             = object_name(r.constid),
  18680.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18681.     from
  18682.         sysobjects o1, sysobjects o2,
  18683.         syscolumns c1, syscolumns c2,
  18684.         sysreferences r, sysindexes i
  18685.     where   
  18686.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18687.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18688.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18689.     and o1.id = r.rkeyid
  18690.     and r.keycnt >= 6
  18691.     and o1.id = c1.id
  18692.     and c1.colid = r.rkey6
  18693.     and r.fkeyid = o2.id
  18694.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18695.     and o2.id = c2.id   
  18696.     and c2.colid = r.fkey6
  18697.     and permissions(o1.id) <> 0
  18698.     and permissions(o2.id) <> 0
  18699.     and i.id = r.rkeyid
  18700.     and i.indid = r.rkeyindid       
  18701.     union all
  18702.     select
  18703.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18704.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18705.         PK_TABLE_NAME       = o1.name,
  18706.         PK_COLUMN_NAME      = c1.name,
  18707.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18708.         PK_COLUMN_PROPID    = convert(int,null),
  18709.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18710.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18711.         FK_TABLE_NAME       = o2.name,
  18712.         FK_COLUMN_NAME      = c2.name,
  18713.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18714.         FK_COLUMN_PROPID    = convert(int,null),
  18715.         ORDINAL             = convert(int,7),
  18716.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18717.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18718.         PK_NAME             = i.name,
  18719.         FK_NAME             = object_name(r.constid),
  18720.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18721.     from
  18722.         sysobjects o1, sysobjects o2,
  18723.         syscolumns c1, syscolumns c2,
  18724.         sysreferences r, sysindexes i
  18725.     where   
  18726.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18727.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18728.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18729.     and o1.id = r.rkeyid
  18730.     and r.keycnt >= 7
  18731.     and o1.id = c1.id
  18732.     and c1.colid = r.rkey7
  18733.     and r.fkeyid = o2.id
  18734.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18735.     and o2.id = c2.id   
  18736.     and c2.colid = r.fkey7
  18737.     and permissions(o1.id) <> 0
  18738.     and permissions(o2.id) <> 0
  18739.     and i.id = r.rkeyid
  18740.     and i.indid = r.rkeyindid       
  18741.     union all
  18742.     select
  18743.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18744.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18745.         PK_TABLE_NAME       = o1.name,
  18746.         PK_COLUMN_NAME      = c1.name,
  18747.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18748.         PK_COLUMN_PROPID    = convert(int,null),
  18749.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18750.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18751.         FK_TABLE_NAME       = o2.name,
  18752.         FK_COLUMN_NAME      = c2.name,
  18753.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18754.         FK_COLUMN_PROPID    = convert(int,null),
  18755.         ORDINAL             = convert(int,8),
  18756.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18757.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18758.         PK_NAME             = i.name,
  18759.         FK_NAME             = object_name(r.constid),
  18760.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18761.     from
  18762.         sysobjects o1, sysobjects o2,
  18763.         syscolumns c1, syscolumns c2,
  18764.         sysreferences r, sysindexes i
  18765.     where   
  18766.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18767.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18768.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18769.     and o1.id = r.rkeyid
  18770.     and r.keycnt >= 8
  18771.     and o1.id = c1.id
  18772.     and c1.colid = r.rkey8
  18773.     and r.fkeyid = o2.id
  18774.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18775.     and o2.id = c2.id   
  18776.     and c2.colid = r.fkey8
  18777.     and permissions(o1.id) <> 0
  18778.     and permissions(o2.id) <> 0
  18779.     and i.id = r.rkeyid
  18780.     and i.indid = r.rkeyindid       
  18781.     union all
  18782.     select
  18783.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18784.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18785.         PK_TABLE_NAME       = o1.name,
  18786.         PK_COLUMN_NAME      = c1.name,
  18787.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18788.         PK_COLUMN_PROPID    = convert(int,null),
  18789.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18790.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18791.         FK_TABLE_NAME       = o2.name,
  18792.         FK_COLUMN_NAME      = c2.name,
  18793.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18794.         FK_COLUMN_PROPID    = convert(int,null),
  18795.         ORDINAL             = convert(int,9),
  18796.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18797.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18798.         PK_NAME             = i.name,
  18799.         FK_NAME             = object_name(r.constid),
  18800.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18801.     from
  18802.         sysobjects o1, sysobjects o2,
  18803.         syscolumns c1, syscolumns c2,
  18804.         sysreferences r, sysindexes i
  18805.     where   
  18806.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18807.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18808.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18809.     and o1.id = r.rkeyid
  18810.     and r.keycnt >= 9
  18811.     and o1.id = c1.id
  18812.     and c1.colid = r.rkey9
  18813.     and r.fkeyid = o2.id
  18814.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18815.     and o2.id = c2.id   
  18816.     and c2.colid = r.fkey9
  18817.     and permissions(o1.id) <> 0
  18818.     and permissions(o2.id) <> 0
  18819.     and i.id = r.rkeyid
  18820.     and i.indid = r.rkeyindid       
  18821.     union all
  18822.     select
  18823.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18824.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18825.         PK_TABLE_NAME       = o1.name,
  18826.         PK_COLUMN_NAME      = c1.name,
  18827.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18828.         PK_COLUMN_PROPID    = convert(int,null),
  18829.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18830.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18831.         FK_TABLE_NAME       = o2.name,
  18832.         FK_COLUMN_NAME      = c2.name,
  18833.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18834.         FK_COLUMN_PROPID    = convert(int,null),
  18835.         ORDINAL             = convert(int,10),
  18836.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18837.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18838.         PK_NAME             = i.name,
  18839.         FK_NAME             = object_name(r.constid),
  18840.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18841.     from
  18842.         sysobjects o1, sysobjects o2,
  18843.         syscolumns c1, syscolumns c2,
  18844.         sysreferences r, sysindexes i
  18845.     where   
  18846.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18847.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18848.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18849.     and o1.id = r.rkeyid
  18850.     and r.keycnt >= 10
  18851.     and o1.id = c1.id
  18852.     and c1.colid = r.rkey10
  18853.     and r.fkeyid = o2.id
  18854.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18855.     and o2.id = c2.id   
  18856.     and c2.colid = r.fkey10
  18857.     and permissions(o1.id) <> 0
  18858.     and permissions(o2.id) <> 0
  18859.     and i.id = r.rkeyid
  18860.     and i.indid = r.rkeyindid       
  18861.     union all
  18862.     select
  18863.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18864.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18865.         PK_TABLE_NAME       = o1.name,
  18866.         PK_COLUMN_NAME      = c1.name,
  18867.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18868.         PK_COLUMN_PROPID    = convert(int,null),
  18869.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18870.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18871.         FK_TABLE_NAME       = o2.name,
  18872.         FK_COLUMN_NAME      = c2.name,
  18873.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18874.         FK_COLUMN_PROPID    = convert(int,null),
  18875.         ORDINAL             = convert(int,11),
  18876.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18877.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18878.         PK_NAME             = i.name,
  18879.         FK_NAME             = object_name(r.constid),
  18880.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18881.     from
  18882.         sysobjects o1, sysobjects o2,
  18883.         syscolumns c1, syscolumns c2,
  18884.         sysreferences r, sysindexes i
  18885.     where   
  18886.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18887.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18888.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18889.     and o1.id = r.rkeyid
  18890.     and r.keycnt >= 11
  18891.     and o1.id = c1.id
  18892.     and c1.colid = r.rkey11
  18893.     and r.fkeyid = o2.id
  18894.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18895.     and o2.id = c2.id   
  18896.     and c2.colid = r.fkey11
  18897.     and permissions(o1.id) <> 0
  18898.     and permissions(o2.id) <> 0
  18899.     and i.id = r.rkeyid
  18900.     and i.indid = r.rkeyindid       
  18901.     union all
  18902.     select
  18903.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18904.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18905.         PK_TABLE_NAME       = o1.name,
  18906.         PK_COLUMN_NAME      = c1.name,
  18907.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18908.         PK_COLUMN_PROPID    = convert(int,null),
  18909.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18910.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18911.         FK_TABLE_NAME       = o2.name,
  18912.         FK_COLUMN_NAME      = c2.name,
  18913.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18914.         FK_COLUMN_PROPID    = convert(int,null),
  18915.         ORDINAL             = convert(int,12),
  18916.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18917.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18918.         PK_NAME             = i.name,
  18919.         FK_NAME             = object_name(r.constid),
  18920.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18921.     from
  18922.         sysobjects o1, sysobjects o2,
  18923.         syscolumns c1, syscolumns c2,
  18924.         sysreferences r, sysindexes i
  18925.     where   
  18926.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18927.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18928.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18929.     and o1.id = r.rkeyid
  18930.     and r.keycnt >= 12
  18931.     and o1.id = c1.id
  18932.     and c1.colid = r.rkey12
  18933.     and r.fkeyid = o2.id
  18934.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18935.     and o2.id = c2.id   
  18936.     and c2.colid = r.fkey12
  18937.     and permissions(o1.id) <> 0
  18938.     and permissions(o2.id) <> 0
  18939.     and i.id = r.rkeyid
  18940.     and i.indid = r.rkeyindid       
  18941.     union all
  18942.     select
  18943.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18944.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18945.         PK_TABLE_NAME       = o1.name,
  18946.         PK_COLUMN_NAME      = c1.name,
  18947.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18948.         PK_COLUMN_PROPID    = convert(int,null),
  18949.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18950.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18951.         FK_TABLE_NAME       = o2.name,
  18952.         FK_COLUMN_NAME      = c2.name,
  18953.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18954.         FK_COLUMN_PROPID    = convert(int,null),
  18955.         ORDINAL             = convert(int,13),
  18956.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18957.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18958.         PK_NAME             = i.name,
  18959.         FK_NAME             = object_name(r.constid),
  18960.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18961.     from
  18962.         sysobjects o1, sysobjects o2,
  18963.         syscolumns c1, syscolumns c2,
  18964.         sysreferences r, sysindexes i
  18965.     where   
  18966.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18967.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18968.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18969.     and o1.id = r.rkeyid
  18970.     and r.keycnt >= 13
  18971.     and o1.id = c1.id
  18972.     and c1.colid = r.rkey13
  18973.     and r.fkeyid = o2.id
  18974.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18975.     and o2.id = c2.id   
  18976.     and c2.colid = r.fkey13
  18977.     and permissions(o1.id) <> 0
  18978.     and permissions(o2.id) <> 0
  18979.     and i.id = r.rkeyid
  18980.     and i.indid = r.rkeyindid       
  18981.     union all
  18982.     select
  18983.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18984.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18985.         PK_TABLE_NAME       = o1.name,
  18986.         PK_COLUMN_NAME      = c1.name,
  18987.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18988.         PK_COLUMN_PROPID    = convert(int,null),
  18989.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18990.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18991.         FK_TABLE_NAME       = o2.name,
  18992.         FK_COLUMN_NAME      = c2.name,
  18993.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18994.         FK_COLUMN_PROPID    = convert(int,null),
  18995.         ORDINAL             = convert(int,14),
  18996.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18997.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18998.         PK_NAME             = i.name,
  18999.         FK_NAME             = object_name(r.constid),
  19000.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  19001.     from
  19002.         sysobjects o1, sysobjects o2,
  19003.         syscolumns c1, syscolumns c2,
  19004.         sysreferences r, sysindexes i
  19005.     where   
  19006.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  19007.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  19008.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  19009.     and o1.id = r.rkeyid
  19010.     and r.keycnt >= 14
  19011.     and o1.id = c1.id
  19012.     and c1.colid = r.rkey14
  19013.     and r.fkeyid = o2.id
  19014.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  19015.     and o2.id = c2.id   
  19016.     and c2.colid = r.fkey14
  19017.     and permissions(o1.id) <> 0
  19018.     and permissions(o2.id) <> 0
  19019.     and i.id = r.rkeyid
  19020.     and i.indid = r.rkeyindid       
  19021.     union all
  19022.     select
  19023.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  19024.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  19025.         PK_TABLE_NAME       = o1.name,
  19026.         PK_COLUMN_NAME      = c1.name,
  19027.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  19028.         PK_COLUMN_PROPID    = convert(int,null),
  19029.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  19030.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  19031.         FK_TABLE_NAME       = o2.name,
  19032.         FK_COLUMN_NAME      = c2.name,
  19033.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  19034.         FK_COLUMN_PROPID    = convert(int,null),
  19035.         ORDINAL             = convert(int,15),
  19036.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  19037.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  19038.         PK_NAME             = i.name,
  19039.         FK_NAME             = object_name(r.constid),
  19040.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  19041.     from
  19042.         sysobjects o1, sysobjects o2,
  19043.         syscolumns c1, syscolumns c2,
  19044.         sysreferences r, sysindexes i
  19045.     where   
  19046.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  19047.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  19048.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  19049.     and o1.id = r.rkeyid
  19050.     and r.keycnt >= 15
  19051.     and o1.id = c1.id
  19052.     and c1.colid = r.rkey15
  19053.     and r.fkeyid = o2.id
  19054.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  19055.     and o2.id = c2.id   
  19056.     and c2.colid = r.fkey15
  19057.     and permissions(o1.id) <> 0
  19058.     and permissions(o2.id) <> 0
  19059.     and i.id = r.rkeyid
  19060.     and i.indid = r.rkeyindid       
  19061.     union all
  19062.     select
  19063.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  19064.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  19065.         PK_TABLE_NAME       = o1.name,
  19066.         PK_COLUMN_NAME      = c1.name,
  19067.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  19068.         PK_COLUMN_PROPID    = convert(int,null),
  19069.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  19070.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  19071.         FK_TABLE_NAME       = o2.name,
  19072.         FK_COLUMN_NAME      = c2.name,
  19073.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  19074.         FK_COLUMN_PROPID    = convert(int,null),
  19075.         ORDINAL             = convert(int,16),
  19076.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  19077.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  19078.         PK_NAME             = i.name,
  19079.         FK_NAME             = object_name(r.constid),
  19080.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  19081.     from
  19082.         sysobjects o1, sysobjects o2,
  19083.         syscolumns c1, syscolumns c2,
  19084.         sysreferences r, sysindexes i
  19085.     where   
  19086.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  19087.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  19088.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  19089.     and o1.id = r.rkeyid
  19090.     and o1.id = c1.id
  19091.     and r.keycnt >= 16
  19092.     and c1.colid = r.rkey16
  19093.     and r.fkeyid = o2.id
  19094.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  19095.     and o2.id = c2.id   
  19096.     and c2.colid = r.fkey16
  19097.     and permissions(o1.id) <> 0
  19098.     and permissions(o2.id) <> 0
  19099.     and i.id = r.rkeyid
  19100.     and i.indid = r.rkeyindid       
  19101.     order by 8,9,2,3,13
  19102. go
  19103. dump tran master with no_log
  19104. go
  19105. create procedure sp_foreign_keys_rowset;5
  19106.     (
  19107.     @server_name        sysname,
  19108.     @pk_catalog_name    sysname = null,
  19109.     @fk_catalog_name    sysname = null,
  19110.     @pk_table_name      sysname = null,
  19111.     @pk_table_schema    sysname = null,
  19112.     @fk_table_name      sysname = null,
  19113.     @fk_table_schema    sysname = null
  19114.     )
  19115. as
  19116.     select
  19117.         PK_TABLE_CATALOG,
  19118.         PK_TABLE_SCHEMA,    
  19119.         PK_TABLE_NAME,  
  19120.         PK_COLUMN_NAME,     
  19121.         PK_COLUMN_GUID, 
  19122.         PK_COLUMN_PROPID,
  19123.         FK_TABLE_CATALOG,
  19124.         FK_TABLE_SCHEMA, 
  19125.         FK_TABLE_NAME,  
  19126.         FK_COLUMN_NAME,     
  19127.         FK_COLUMN_GUID, 
  19128.         FK_COLUMN_PROPID,
  19129.         ORDINAL,        
  19130.         UPDATE_RULE,    
  19131.         DELETE_RULE
  19132.         --PK_NAME,      
  19133.         --FK_NAME,
  19134.         --DEFERRABILITY 
  19135.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  19136.                 @server_name,
  19137.                 @pk_catalog_name,
  19138.                 @pk_table_schema,
  19139.                 @pk_table_name,
  19140.                 @fk_catalog_name,
  19141.                 @fk_table_schema,
  19142.                 @fk_table_name >
  19143.     order by 7,8,9,1,2,3,13
  19144. go
  19145.  
  19146. grant execute on sp_foreign_keys_rowset to public
  19147. go
  19148.  
  19149. dump tran master with no_log
  19150. go
  19151. if (charindex('6.00', @@version) > 0)
  19152.     begin
  19153.     if (exists (select * from sysobjects
  19154.             where name = 'sp_foreign_keys_rowset' and type = 'P '))
  19155.         begin
  19156.         drop procedure sp_foreign_keys_rowset
  19157.         dump tran master with no_log
  19158.         end
  19159.     end
  19160. go
  19161.  
  19162.  
  19163. print ''
  19164. print 'creating sp_indexes_rowset'
  19165. go
  19166.  
  19167. /*  6.0 and 6.5 version */
  19168. create procedure sp_indexes_rowset
  19169.     (
  19170.     @table_name varchar(255), 
  19171.     @index_name varchar(255) = null,
  19172.     @table_schema   varchar(255) = null  
  19173.     )
  19174. as
  19175.     select  TABLE_CATALOG       = db_name(),
  19176.         TABLE_SCHEMA        = user_name(o.uid),
  19177.         TABLE_NAME      = o.name,
  19178.         INDEX_CATALOG       = db_name(),        
  19179.         INDEX_SCHEMA        = user_name(o.uid),
  19180.         INDEX_NAME      = x.name,
  19181.         PRIMARY_KEY     = convert(bit,(x.status & 0x800)/0x800),
  19182.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19183.         "CLUSTERED"     = convert(bit,(x.status & 16)/16),
  19184.         "TYPE"          = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19185.         FILL_FACTOR     = convert(int, x.OrigFillFactor),
  19186.         INITIAL_SIZE        = convert(int,null),
  19187.         NULLS           = convert(int,null),
  19188.         SORT_BOOKMARKS      = convert(bit,0),
  19189.         AUTO_UPDATE     = convert(bit,1),
  19190.         NULL_COLLATION      = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19191.         ORDINAL_POSITION    = convert(int,
  19192.                        (
  19193.                         select count(*)
  19194.                         from syscolumns sc
  19195.                         where sc.id     =  c.id
  19196.                           AND sc.number =  c.number
  19197.                           AND sc.colid  <= c.colid
  19198.                         )),
  19199.         COLUMN_NAME     = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  19200.         COLUMN_GUID     = convert(binary(16),null),
  19201.         COLUMN_PROPID       = convert(int,null),
  19202.         COLLATION   = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19203.         CARDINALITY     = case when (x.status & 2) = 2 then x.rows else null end,
  19204.         PAGES           = convert(int, x.dpages),
  19205.         FILTER_CONDITION    = convert(varchar(1),null),
  19206.         INTEGRATED      = convert(bit,(x.status & 16)/16)
  19207.         
  19208.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  19209.     where   o.type in ('U')
  19210.     and     o.name = @table_name
  19211.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19212.     and (@index_name is null or @index_name = x.name)
  19213.     and     x.id = o.id
  19214.     and     x.id = c.id
  19215.     and     c.colid < x.keycnt+(x.status&16)/16
  19216.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19217.     and     (
  19218.         suser_id() = 1   /* User is the System Administrator */
  19219.         or o.uid = user_id()     /* User created the object */
  19220.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19221.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19222.             from sysprotects p
  19223.             where p.id = o.id
  19224.             /* get rows for public,current user,user's group */
  19225.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19226.             /* check for SELECT,EXECUTE privilege */
  19227.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19228.             ) = 1   /* final magic...compare Grants   */
  19229.            )
  19230.     order by 8 desc, 4, 5, 6, 17
  19231. go
  19232. dump tran master with no_log
  19233. go
  19234. create procedure sp_indexes_rowset;2
  19235.     (
  19236.     @index_name varchar(255) = null,
  19237.     @table_schema   varchar(255) = null  
  19238.     )
  19239. as
  19240.     select  TABLE_CATALOG       = db_name(),
  19241.         TABLE_SCHEMA        = user_name(o.uid),
  19242.         TABLE_NAME      = o.name,
  19243.         INDEX_CATALOG       = db_name(),        
  19244.         INDEX_SCHEMA        = user_name(o.uid),
  19245.         INDEX_NAME      = x.name,
  19246.         PRIMARY_KEY     = convert(bit,(x.status & 0x800)/0x800),
  19247.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19248.         "CLUSTERED"     = convert(bit,(x.status & 16)/16),
  19249.         "TYPE"          = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19250.         FILL_FACTOR     = convert(int, x.OrigFillFactor),
  19251.         INITIAL_SIZE        = convert(int,null),
  19252.         NULLS           = convert(int,null),
  19253.         SORT_BOOKMARKS      = convert(bit,0),
  19254.         AUTO_UPDATE     = convert(bit,1),
  19255.         NULL_COLLATION      = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19256.         ORDINAL_POSITION    = convert(int,
  19257.                        (
  19258.                         select count(*)
  19259.                         from syscolumns sc
  19260.                         where sc.id     =  c.id
  19261.                           AND sc.number =  c.number
  19262.                           AND sc.colid  <= c.colid
  19263.                         )),
  19264.         COLUMN_NAME     = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  19265.         COLUMN_GUID     = convert(binary(16),null),
  19266.         COLUMN_PROPID       = convert(int,null),
  19267.         COLLATION   = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19268.         CARDINALITY     = case when (x.status & 2) = 2 then x.rows else null end,
  19269.         PAGES           = convert(int, x.dpages),
  19270.         FILTER_CONDITION    = convert(varchar(1),null),
  19271.         INTEGRATED      = convert(bit,(x.status & 16)/16) 
  19272.         
  19273.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  19274.     where   o.type in ('U')
  19275.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19276.     and (@index_name is null or @index_name = x.name)
  19277.     and     x.id = o.id
  19278.     and     x.id = c.id
  19279.     and     c.colid < x.keycnt+(x.status&16)/16
  19280.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19281.     and     (
  19282.         suser_id() = 1   /* User is the System Administrator */
  19283.         or o.uid = user_id()     /* User created the object */
  19284.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19285.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19286.             from sysprotects p
  19287.             where p.id = o.id
  19288.             /* get rows for public,current user,user's group */
  19289.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19290.             /* check for SELECT,EXECUTE privilege */
  19291.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19292.             ) = 1   /* final magic...compare Grants   */
  19293.            )
  19294.     order by 8 desc, 4, 5, 6, 17
  19295. go
  19296. dump tran master with no_log
  19297. go
  19298.  
  19299. if (charindex('7.00', @@version) > 0 or
  19300.     charindex('8.00', @@version) > 0)
  19301.     drop procedure sp_indexes_rowset
  19302. else
  19303. begin
  19304.     print ''
  19305.     print ''
  19306.     print 'Warning:'
  19307.     print 'you are installing the stored procedures '
  19308.     print 'on a pre 7.0 SQL Server.'
  19309.     print 'Ignore the following errors.'
  19310. end
  19311. go
  19312.  
  19313. /*  7.0 version */
  19314. create procedure sp_indexes_rowset
  19315.     (
  19316.     @table_name sysname, 
  19317.     @index_name sysname = null,
  19318.     @table_schema   sysname = null   
  19319.     )
  19320. as
  19321.     select  TABLE_CATALOG       = db_name(),
  19322.         TABLE_SCHEMA        = user_name(o.uid),
  19323.         TABLE_NAME      = o.name,
  19324.         INDEX_CATALOG       = db_name(),        
  19325.         INDEX_SCHEMA        = user_name(o.uid),
  19326.         INDEX_NAME      = x.name,
  19327.         PRIMARY_KEY     = convert(bit,(x.status & 0x800)/0x800),
  19328.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19329.         "CLUSTERED"     = convert(bit,(x.status & 16)/16),
  19330.         "TYPE"          = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19331.         FILL_FACTOR     = convert(int, x.OrigFillFactor),
  19332.         INITIAL_SIZE        = convert(int,null),
  19333.         NULLS           = convert(int,null),
  19334.         SORT_BOOKMARKS      = convert(bit,0),
  19335.         AUTO_UPDATE     = convert(bit,1),
  19336.         NULL_COLLATION      = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19337.         ORDINAL_POSITION    = convert(int, xk.keyno),
  19338.         COLUMN_NAME     = c.name,
  19339.         COLUMN_GUID     = convert(uniqueidentifier,null),
  19340.         COLUMN_PROPID       = convert(int,null),
  19341.         COLLATION   = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19342.         CARDINALITY     = case when (x.status & 2) = 2 then x.rows else null end,
  19343.         PAGES           = convert(int, x.dpages),
  19344.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19345.         INTEGRATED      = convert(bit,(x.status & 16)/16) 
  19346.         
  19347.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19348.     where   o.type in ('U')
  19349.     and     o.name = @table_name
  19350.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19351.     and (@index_name is null or @index_name = x.name)
  19352.     and x.id = o.id
  19353.     and o.id = c.id
  19354.     and o.id = xk.id
  19355.     and x.indid = xk.indid
  19356.     and c.colid = xk.colid
  19357.     and xk.keyno <= x.keycnt
  19358.     and permissions(o.id, c.name) <> 0
  19359.     and     (x.status&32) = 0  -- No hypothetical indexes
  19360.     order by 8 desc, 4, 5, 6, 17
  19361. go
  19362. dump tran master with no_log
  19363. go
  19364. create procedure sp_indexes_rowset;2
  19365.     (
  19366.     @index_name sysname = null,
  19367.     @table_schema   sysname = null   
  19368.     )
  19369. as
  19370.     select  TABLE_CATALOG       = db_name(),
  19371.         TABLE_SCHEMA        = user_name(o.uid),
  19372.         TABLE_NAME      = o.name,
  19373.         INDEX_CATALOG       = db_name(),        
  19374.         INDEX_SCHEMA        = user_name(o.uid),
  19375.         INDEX_NAME      = x.name,
  19376.         PRIMARY_KEY     = convert(bit,(x.status & 0x800)/0x800),
  19377.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19378.         "CLUSTERED"     = convert(bit,(x.status & 16)/16),
  19379.         "TYPE"          = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19380.         FILL_FACTOR     = convert(int, x.OrigFillFactor),
  19381.         INITIAL_SIZE        = convert(int,null),
  19382.         NULLS           = convert(int,null),
  19383.         SORT_BOOKMARKS      = convert(bit,0),
  19384.         AUTO_UPDATE     = convert(bit,1),
  19385.         NULL_COLLATION      = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19386.         ORDINAL_POSITION    = convert(int, xk.keyno),
  19387.         COLUMN_NAME     = c.name,
  19388.         COLUMN_GUID     = convert(uniqueidentifier,null),
  19389.         COLUMN_PROPID       = convert(int,null),
  19390.         COLLATION   = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19391.         CARDINALITY     = case when (x.status & 2) = 2 then x.rows else null end,
  19392.         PAGES           = convert(int, x.dpages),
  19393.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19394.         INTEGRATED      = convert(bit,(x.status & 16)/16) 
  19395.         
  19396.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19397.     where   o.type in ('U')
  19398.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19399.     and (@index_name is null or @index_name = x.name)
  19400.     and x.id = o.id
  19401.     and o.id = c.id
  19402.     and o.id = xk.id
  19403.     and x.indid = xk.indid
  19404.     and c.colid = xk.colid
  19405.     and xk.keyno <= x.keycnt
  19406.     and permissions(o.id, c.name) <> 0
  19407.     and (x.status&32) = 0  -- No hypothetical indexes
  19408.     order by 8 desc, 4, 5, 6, 17
  19409. go
  19410. dump tran master with no_log
  19411. go
  19412. create procedure sp_indexes_rowset;5
  19413.     (
  19414.     @table_server       sysname,
  19415.     @table_catalog      sysname = null,
  19416.     @table_name     sysname = null,
  19417.     @index_name     sysname = null,
  19418.     @table_schema       sysname = null
  19419.     )
  19420. as
  19421.     select
  19422.         TABLE_CATALOG,  
  19423.         TABLE_SCHEMA,   
  19424.         TABLE_NAME, 
  19425.         INDEX_CATALOG,      
  19426.         INDEX_SCHEMA,   
  19427.         INDEX_NAME, 
  19428.         PRIMARY_KEY,    
  19429.         "UNIQUE",   
  19430.         "CLUSTERED",    
  19431.         "TYPE",     
  19432.         FILL_FACTOR,    
  19433.         INITIAL_SIZE,   
  19434.         NULLS,      
  19435.         SORT_BOOKMARKS, 
  19436.         AUTO_UPDATE,    
  19437.         NULL_COLLATION, 
  19438.         ORDINAL_POSITION,
  19439.         COLUMN_NAME,    
  19440.         COLUMN_GUID,    
  19441.         COLUMN_PROPID,  
  19442.         COLLATION,  
  19443.         CARDINALITY,    
  19444.         PAGES,      
  19445.         FILTER_CONDITION
  19446.     --  INTEGRATED
  19447.     from master.dbo.SYSREMOTE_INDEXES <
  19448.                 @table_server,
  19449.                 @table_catalog,
  19450.                 @table_schema,
  19451.                 @index_name,
  19452.                 NULL,           /* TYPE (index type) */
  19453.                 @table_name >
  19454.     order by 8 desc, 4, 5, 6, 17
  19455. go
  19456.  
  19457. grant execute on sp_indexes_rowset to public
  19458. go
  19459.  
  19460. dump tran master with no_log
  19461. go
  19462.  
  19463. if (charindex('8.00', @@version) > 0)
  19464.     drop procedure sp_indexes_rowset
  19465. else
  19466. begin
  19467.     print ''
  19468.     print ''
  19469.     print 'Warning:'
  19470.     print 'you are installing the stored procedures '
  19471.     print 'on a pre 8.0 SQL Server.'
  19472.     print 'Ignore the following errors.'
  19473. end
  19474. go
  19475.  
  19476. /*  8.0 version */
  19477. create procedure sp_indexes_rowset
  19478.     (
  19479.     @table_name sysname, 
  19480.     @index_name sysname = null,
  19481.     @table_schema   sysname = null   
  19482.     )
  19483. as
  19484.     select  TABLE_CATALOG       = db_name(),
  19485.         TABLE_SCHEMA        = user_name(o.uid),
  19486.         TABLE_NAME      = o.name,
  19487.         INDEX_CATALOG       = db_name(),        
  19488.         INDEX_SCHEMA        = user_name(o.uid),
  19489.         INDEX_NAME      = x.name,
  19490.         PRIMARY_KEY     = convert(bit,(x.status & 0x800)/0x800),
  19491.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19492.         "CLUSTERED"     = convert(bit,(x.status & 16)/16),
  19493.         "TYPE"          = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19494.         FILL_FACTOR     = convert(int, x.OrigFillFactor),
  19495.         INITIAL_SIZE        = convert(int,null),
  19496.         NULLS           = convert(int,null),
  19497.         SORT_BOOKMARKS      = convert(bit,0),
  19498.         AUTO_UPDATE     = convert(bit,1),
  19499.         NULL_COLLATION      = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19500.         ORDINAL_POSITION    = convert(int, xk.keyno),
  19501.         COLUMN_NAME     = c.name,
  19502.         COLUMN_GUID     = convert(uniqueidentifier,null),
  19503.         COLUMN_PROPID       = convert(int,null),
  19504.         COLLATION   = convert(smallint,
  19505.                         case when indexkey_property(o.id, x.indid, xk.keyno, 'IsDescending') =1
  19506.                         then 2      /* DB_COLLATION_DESC */ 
  19507.                         else 1      /* DB_COLLATION_ASC */ 
  19508.                         end),
  19509.         CARDINALITY     = case when (x.status & 2) = 2 then x.rows else null end,
  19510.         PAGES           = convert(int, x.dpages),
  19511.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19512.         INTEGRATED      = convert(bit,(x.status & 16)/16) 
  19513.         
  19514.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19515.     where   o.type in ('U')
  19516.     and     o.name = @table_name
  19517.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19518.     and (@index_name is null or @index_name = x.name)
  19519.     and x.id = o.id
  19520.     and o.id = c.id
  19521.     and o.id = xk.id
  19522.     and x.indid = xk.indid
  19523.     and c.colid = xk.colid
  19524.     and xk.keyno <= x.keycnt
  19525.     and permissions(o.id, c.name) <> 0
  19526.     and     (x.status&32) = 0  -- No hypothetical indexes
  19527.     order by 8 desc, 4, 5, 6, 17
  19528. go
  19529. dump tran master with no_log
  19530. go
  19531. create procedure sp_indexes_rowset;2
  19532.     (
  19533.     @index_name sysname = null,
  19534.     @table_schema   sysname = null   
  19535.     )
  19536. as
  19537.     select  TABLE_CATALOG       = db_name(),
  19538.         TABLE_SCHEMA        = user_name(o.uid),
  19539.         TABLE_NAME      = o.name,
  19540.         INDEX_CATALOG       = db_name(),        
  19541.         INDEX_SCHEMA        = user_name(o.uid),
  19542.         INDEX_NAME      = x.name,
  19543.         PRIMARY_KEY     = convert(bit,(x.status & 0x800)/0x800),
  19544.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19545.         "CLUSTERED"     = convert(bit,(x.status & 16)/16),
  19546.         "TYPE"          = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19547.         FILL_FACTOR     = convert(int, x.OrigFillFactor),
  19548.         INITIAL_SIZE        = convert(int,null),
  19549.         NULLS           = convert(int,null),
  19550.         SORT_BOOKMARKS      = convert(bit,0),
  19551.         AUTO_UPDATE     = convert(bit,1),
  19552.         NULL_COLLATION      = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19553.         ORDINAL_POSITION    = convert(int, xk.keyno),
  19554.         COLUMN_NAME     = c.name,
  19555.         COLUMN_GUID     = convert(uniqueidentifier,null),
  19556.         COLUMN_PROPID       = convert(int,null),
  19557.         COLLATION   = convert(smallint,
  19558.                         case when indexkey_property(o.id, x.indid, xk.keyno, 'IsDescending') =1
  19559.                         then 2      /* DB_COLLATION_DESC */ 
  19560.                         else 1      /* DB_COLLATION_ASC */ 
  19561.                         end),
  19562.         CARDINALITY     = case when (x.status & 2) = 2 then x.rows else null end,
  19563.         PAGES           = convert(int, x.dpages),
  19564.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19565.         INTEGRATED      = convert(bit,(x.status & 16)/16) 
  19566.         
  19567.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19568.     where   o.type in ('U')
  19569.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19570.     and (@index_name is null or @index_name = x.name)
  19571.     and x.id = o.id
  19572.     and o.id = c.id
  19573.     and o.id = xk.id
  19574.     and x.indid = xk.indid
  19575.     and c.colid = xk.colid
  19576.     and xk.keyno <= x.keycnt
  19577.     and permissions(o.id, c.name) <> 0
  19578.     and (x.status&32) = 0  -- No hypothetical indexes
  19579.     order by 8 desc, 4, 5, 6, 17
  19580. go
  19581. dump tran master with no_log
  19582. go
  19583. create procedure sp_indexes_rowset;5
  19584.     (
  19585.     @table_server       sysname,
  19586.     @table_catalog      sysname = null,
  19587.     @table_name     sysname = null,
  19588.     @index_name     sysname = null,
  19589.     @table_schema       sysname = null
  19590.     )
  19591. as
  19592.     select
  19593.         TABLE_CATALOG,  
  19594.         TABLE_SCHEMA,   
  19595.         TABLE_NAME, 
  19596.         INDEX_CATALOG,      
  19597.         INDEX_SCHEMA,   
  19598.         INDEX_NAME, 
  19599.         PRIMARY_KEY,    
  19600.         "UNIQUE",   
  19601.         "CLUSTERED",    
  19602.         "TYPE",     
  19603.         FILL_FACTOR,    
  19604.         INITIAL_SIZE,   
  19605.         NULLS,      
  19606.         SORT_BOOKMARKS, 
  19607.         AUTO_UPDATE,    
  19608.         NULL_COLLATION, 
  19609.         ORDINAL_POSITION,
  19610.         COLUMN_NAME,    
  19611.         COLUMN_GUID,    
  19612.         COLUMN_PROPID,  
  19613.         COLLATION,  
  19614.         CARDINALITY,    
  19615.         PAGES,      
  19616.         FILTER_CONDITION
  19617.     --  INTEGRATED
  19618.     from master.dbo.SYSREMOTE_INDEXES <
  19619.                 @table_server,
  19620.                 @table_catalog,
  19621.                 @table_schema,
  19622.                 @index_name,
  19623.                 NULL,           /* TYPE (index type) */
  19624.                 @table_name >
  19625.     order by 8 desc, 4, 5, 6, 17
  19626. go
  19627.  
  19628. grant execute on sp_indexes_rowset to public
  19629. go
  19630.  
  19631. dump tran master with no_log
  19632. go
  19633. if (charindex('6.00', @@version) > 0)
  19634.     begin
  19635.     if (exists (select * from sysobjects
  19636.             where name = 'sp_indexes_rowset' and type = 'P '))
  19637.         begin
  19638.         drop procedure sp_indexes_rowset
  19639.         dump tran master with no_log
  19640.         end
  19641.     end
  19642. go
  19643.  
  19644.  
  19645. print ''
  19646. print 'creating sp_primary_keys_rowset'
  19647. go
  19648.  
  19649. /*  Procedure for 6.0 and 6.5 servers */
  19650. create procedure sp_primary_keys_rowset
  19651.     (
  19652.     @table_name     varchar(255),
  19653.     @table_schema       varchar(244) = null
  19654.     )
  19655. as
  19656.     select  TABLE_CATALOG   = db_name(),
  19657.             TABLE_SCHEMA    = user_name(o.uid),
  19658.             TABLE_NAME      = o.name,   
  19659.             COLUMN_NAME     = c.name,
  19660.             COLUMN_GUID     = convert(binary(16),null),
  19661.             COLUMN_PROPID   = convert(int,null),
  19662.             ORDINAL         = convert(int,
  19663.                                       case
  19664.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  1) then 1
  19665.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  2) then 2
  19666.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  3) then 3
  19667.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  4) then 4
  19668.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  5) then 5
  19669.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  6) then 6
  19670.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  7) then 7
  19671.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  8) then 8
  19672.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  9) then 9
  19673.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 10) then 10
  19674.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 11) then 11
  19675.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 12) then 12
  19676.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 13) then 13
  19677.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 14) then 14
  19678.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 15) then 15
  19679.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 16) then 16
  19680.                                       end
  19681.                                      ),
  19682.             PK_NAME         = i.name
  19683.     from    sysindexes i, syscolumns c, sysobjects o, sysusers u
  19684.     where   o.type in ('U')
  19685.     and     o.name = @table_name
  19686.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19687.     and     o.id = c.id
  19688.     and     o.id = i.id
  19689.     and     (i.status & 0x800) = 0x800
  19690.     and (c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  1) or
  19691.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  2) or
  19692.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  3) or
  19693.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  4) or
  19694.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  5) or
  19695.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  6) or
  19696.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  7) or
  19697.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  8) or
  19698.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  9) or
  19699.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 10) or
  19700.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 11) or
  19701.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 12) or
  19702.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 13) or
  19703.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 14) or
  19704.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 15) or
  19705.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 16)
  19706.         )
  19707.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19708.     and     (
  19709.         suser_id() = 1   /* User is the System Administrator */
  19710.         or o.uid = user_id()     /* User created the object */
  19711.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19712.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19713.             from sysprotects p
  19714.             where p.id = o.id
  19715.             /* get rows for public,current user,user's group */
  19716.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19717.             /* check for SELECT,EXECUTE privilege */
  19718.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19719.             ) = 1   /* final magic...compare Grants   */
  19720.            )
  19721.     order by 2, 3
  19722. go
  19723. dump tran master with no_log
  19724. go
  19725. create procedure sp_primary_keys_rowset;2
  19726.     (
  19727.     @table_schema       varchar(244) = null
  19728.     )
  19729. as
  19730.     select  TABLE_CATALOG   = db_name(),
  19731.             TABLE_SCHEMA    = user_name(o.uid),
  19732.             TABLE_NAME      = o.name,   
  19733.             COLUMN_NAME     = c.name,
  19734.             COLUMN_GUID     = convert(binary(16),null),
  19735.             COLUMN_PROPID   = convert(int,null),
  19736.             ORDINAL         = convert(int,
  19737.                                       case
  19738.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  1) then 1
  19739.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  2) then 2
  19740.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  3) then 3
  19741.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  4) then 4
  19742.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  5) then 5
  19743.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  6) then 6
  19744.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  7) then 7
  19745.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  8) then 8
  19746.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  9) then 9
  19747.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 10) then 10
  19748.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 11) then 11
  19749.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 12) then 12
  19750.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 13) then 13
  19751.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 14) then 14
  19752.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 15) then 15
  19753.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 16) then 16
  19754.                                       end
  19755.                                      ),
  19756.             PK_NAME         = i.name
  19757.     from    sysindexes i, syscolumns c, sysobjects o, sysusers u
  19758.     where   o.type in ('U')
  19759.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19760.     and     o.id = c.id
  19761.     and     o.id = i.id
  19762.     and     (i.status & 0x800) = 0x800
  19763.     and (c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  1) or
  19764.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  2) or
  19765.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  3) or
  19766.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  4) or
  19767.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  5) or
  19768.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  6) or
  19769.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  7) or
  19770.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  8) or
  19771.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  9) or
  19772.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 10) or
  19773.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 11) or
  19774.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 12) or
  19775.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 13) or
  19776.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 14) or
  19777.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 15) or
  19778.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 16)
  19779.         )
  19780.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19781.     and     (
  19782.         suser_id() = 1   /* User is the System Administrator */
  19783.         or o.uid = user_id()     /* User created the object */
  19784.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19785.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19786.             from sysprotects p
  19787.             where p.id = o.id
  19788.             /* get rows for public,current user,user's group */
  19789.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19790.             /* check for SELECT,EXECUTE privilege */
  19791.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19792.             ) = 1   /* final magic...compare Grants   */
  19793.            )
  19794.     order by 2, 3
  19795. go
  19796.  
  19797. if (charindex('7.00', @@version) = 0 and
  19798.     charindex('8.00', @@version) = 0)
  19799. begin
  19800.     print ''
  19801.     print ''
  19802.     print 'Warning:'
  19803.     print 'you are installing the stored procedures '
  19804.     print 'on a pre 8.0 SQL Server.'
  19805.     print 'Ignore the following errors.'
  19806. end
  19807. else
  19808.     drop proc sp_primary_keys_rowset
  19809. go
  19810.  
  19811. /*  Procedure for 7.0 & 8.0 servers */
  19812. create procedure sp_primary_keys_rowset
  19813.     (
  19814.     @table_name     sysname,
  19815.     @table_schema   sysname = null
  19816.     )
  19817. as
  19818.     select  TABLE_CATALOG   = db_name(),
  19819.             TABLE_SCHEMA    = user_name(o.uid),
  19820.             TABLE_NAME      = o.name,   
  19821.             COLUMN_NAME     = c.name,
  19822.             COLUMN_GUID     = convert(uniqueidentifier,null),
  19823.             COLUMN_PROPID   = convert(int,null),
  19824.             ORDINAL         = convert(int,
  19825.                                       case
  19826.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  1) then 1
  19827.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  2) then 2
  19828.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  3) then 3
  19829.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  4) then 4
  19830.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  5) then 5
  19831.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  6) then 6
  19832.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  7) then 7
  19833.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  8) then 8
  19834.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  9) then 9
  19835.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 10) then 10
  19836.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 11) then 11
  19837.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 12) then 12
  19838.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 13) then 13
  19839.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 14) then 14
  19840.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 15) then 15
  19841.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 16) then 16
  19842.                                       end
  19843.                                      ),
  19844.             PK_NAME         = i.name
  19845.     from    sysindexes i, syscolumns c, sysobjects o
  19846.     where   o.type in ('U')
  19847.     and     o.name = @table_name
  19848.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19849.     and     o.id = c.id
  19850.     and     o.id = i.id
  19851.     and     (i.status & 0x800) = 0x800 -- PRIMARY KEY
  19852.     and (c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  1) or
  19853.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  2) or
  19854.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  3) or
  19855.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  4) or
  19856.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  5) or
  19857.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  6) or
  19858.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  7) or
  19859.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  8) or
  19860.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  9) or
  19861.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 10) or
  19862.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 11) or
  19863.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 12) or
  19864.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 13) or
  19865.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 14) or
  19866.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 15) or
  19867.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 16)
  19868.         )
  19869.     and permissions(o.id) <> 0
  19870.     order by 2, 3
  19871. go
  19872. dump tran master with no_log
  19873. go
  19874. create procedure sp_primary_keys_rowset;2
  19875.     (
  19876.     @table_schema   sysname = null
  19877.     )
  19878. as
  19879.     select  TABLE_CATALOG   = db_name(),
  19880.             TABLE_SCHEMA    = user_name(o.uid),
  19881.             TABLE_NAME      = o.name,   
  19882.             COLUMN_NAME     = c.name,
  19883.             COLUMN_GUID     = convert(uniqueidentifier,null),
  19884.             COLUMN_PROPID   = convert(int,null),
  19885.             ORDINAL         = convert(int,
  19886.                                       case
  19887.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  1) then 1
  19888.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  2) then 2
  19889.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  3) then 3
  19890.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  4) then 4
  19891.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  5) then 5
  19892.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  6) then 6
  19893.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  7) then 7
  19894.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  8) then 8
  19895.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  9) then 9
  19896.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 10) then 10
  19897.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 11) then 11
  19898.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 12) then 12
  19899.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 13) then 13
  19900.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 14) then 14
  19901.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 15) then 15
  19902.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 16) then 16
  19903.                                       end
  19904.                                      ),
  19905.             PK_NAME         = i.name
  19906.     from    sysindexes i, syscolumns c, sysobjects o
  19907.     where   o.type in ('U')
  19908.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19909.     and     o.id = c.id
  19910.     and     o.id = i.id
  19911.     and     (i.status & 0x800) = 0x800 -- PRIMARY KEY
  19912.     and (c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  1) or
  19913.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  2) or
  19914.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  3) or
  19915.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  4) or
  19916.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  5) or
  19917.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  6) or
  19918.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  7) or
  19919.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  8) or
  19920.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  9) or
  19921.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 10) or
  19922.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 11) or
  19923.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 12) or
  19924.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 13) or
  19925.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 14) or
  19926.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 15) or
  19927.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 16)
  19928.         )
  19929.     and permissions(o.id) <> 0
  19930.     order by 2, 3
  19931. go
  19932. dump tran master with no_log
  19933. go
  19934. create procedure sp_primary_keys_rowset;3
  19935.     (
  19936.     @table_name     sysname = null,
  19937.     @table_schema       sysname = null
  19938.     )
  19939. as
  19940. IF @table_name is not NULL
  19941.     BEGIN
  19942.     select  TABLE_CATALOG   = db_name(),
  19943.             TABLE_SCHEMA    = user_name(o.uid),
  19944.             TABLE_NAME      = o.name,   
  19945.             COLUMN_NAME     = c.name,
  19946.             COLUMN_GUID     = convert(uniqueidentifier,null),
  19947.             COLUMN_PROPID   = convert(int,null),
  19948.             ORDINAL         = convert(int,
  19949.                                       case
  19950.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  1) then 1
  19951.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  2) then 2
  19952.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  3) then 3
  19953.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  4) then 4
  19954.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  5) then 5
  19955.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  6) then 6
  19956.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  7) then 7
  19957.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  8) then 8
  19958.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  9) then 9
  19959.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 10) then 10
  19960.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 11) then 11
  19961.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 12) then 12
  19962.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 13) then 13
  19963.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 14) then 14
  19964.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 15) then 15
  19965.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 16) then 16
  19966.                                       end
  19967.                                      ),
  19968.             PK_NAME         = i.name
  19969.     from    sysindexes i, syscolumns c, sysobjects o
  19970.     where   o.type in ('U')
  19971.     and     o.name = @table_name
  19972.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19973.     and     o.id = c.id
  19974.     and     o.id = i.id
  19975.     and     (i.status & 0x800) = 0x800 -- PRIMARY KEY
  19976.     and (c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  1) or
  19977.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  2) or
  19978.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  3) or
  19979.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  4) or
  19980.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  5) or
  19981.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  6) or
  19982.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  7) or
  19983.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  8) or
  19984.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  9) or
  19985.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 10) or
  19986.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 11) or
  19987.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 12) or
  19988.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 13) or
  19989.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 14) or
  19990.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 15) or
  19991.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 16)
  19992.         )
  19993.     and permissions(o.id) <> 0
  19994.     END
  19995. ELSE
  19996.     BEGIN
  19997.     select  TABLE_CATALOG   = db_name(),
  19998.             TABLE_SCHEMA    = user_name(o.uid),
  19999.             TABLE_NAME      = o.name,   
  20000.             COLUMN_NAME     = c.name,
  20001.             COLUMN_GUID     = convert(uniqueidentifier,null),
  20002.             COLUMN_PROPID   = convert(int,null),
  20003.             ORDINAL         = convert(int,
  20004.                                       case
  20005.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  1) then 1
  20006.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  2) then 2
  20007.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  3) then 3
  20008.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  4) then 4
  20009.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  5) then 5
  20010.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  6) then 6
  20011.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  7) then 7
  20012.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  8) then 8
  20013.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  9) then 9
  20014.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 10) then 10
  20015.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 11) then 11
  20016.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 12) then 12
  20017.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 13) then 13
  20018.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 14) then 14
  20019.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 15) then 15
  20020.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 16) then 16
  20021.                                       end
  20022.                                      ),
  20023.             PK_NAME         = i.name
  20024.     from    sysindexes i, syscolumns c, sysobjects o
  20025.     where   o.type in ('U')
  20026.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  20027.     and     o.id = c.id
  20028.     and     o.id = i.id
  20029.     and     (i.status & 0x800) = 0x800 -- PRIMARY KEY
  20030.     and (c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  1) or
  20031.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  2) or
  20032.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  3) or
  20033.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  4) or
  20034.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  5) or
  20035.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  6) or
  20036.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  7) or
  20037.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  8) or
  20038.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  9) or
  20039.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 10) or
  20040.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 11) or
  20041.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 12) or
  20042.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 13) or
  20043.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 14) or
  20044.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 15) or
  20045.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 16)
  20046.         )
  20047.     and permissions(o.id) <> 0
  20048.     END
  20049. go
  20050. dump tran master with no_log
  20051. go
  20052. create procedure sp_primary_keys_rowset;5
  20053.     (
  20054.     @table_server       sysname,
  20055.     @table_catalog      sysname = null,
  20056.     @table_name         sysname = null,
  20057.     @table_schema       sysname = null
  20058.     )
  20059. as
  20060.     select
  20061.         TABLE_CATALOG,  
  20062.         TABLE_SCHEMA,   
  20063.         TABLE_NAME, 
  20064.         COLUMN_NAME,    
  20065.         COLUMN_GUID,    
  20066.         COLUMN_PROPID,  
  20067.         ORDINAL
  20068.         --PK_NAME       
  20069.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  20070.                 @table_server,
  20071.                 @table_catalog,
  20072.                 @table_schema,
  20073.                 @table_name >
  20074.     order by 1,2,3
  20075. go
  20076.  
  20077. grant execute on sp_primary_keys_rowset to public
  20078. go
  20079.  
  20080. dump tran master with no_log
  20081. go
  20082. if (charindex('6.00', @@version) > 0)
  20083.     begin
  20084.     if (exists (select * from sysobjects
  20085.             where name = 'sp_primary_keys_rowset' and type = 'P '))
  20086.         begin
  20087.         drop procedure sp_primary_keys_rowset
  20088.         dump tran master with no_log
  20089.         end
  20090.     end
  20091. go
  20092.  
  20093.  
  20094. print ''
  20095. print 'creating sp_provider_types_rowset'
  20096. go
  20097.  
  20098. /*  Procedure for 6.0 and 6.50 servers */
  20099. create proc sp_provider_types_rowset
  20100.     (
  20101.     @data_type  smallint = null,
  20102.     @best_match tinyint  = null
  20103.     )
  20104. as
  20105.     select
  20106.         TYPE_NAME       = case when t.usertype = 80 then t.name 
  20107.                         else d.type_name 
  20108.                         end,
  20109.         DATA_TYPE       = d.oledb_data_type,                            
  20110.         COLUMN_SIZE     = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  20111.                             then @@max_precision
  20112.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  20113.         LITERAL_PREFIX      = d.literal_prefix,
  20114.         LITERAL_SUFFIX      = d.literal_suffix,
  20115.         CREATE_PARAMS       = convert(varchar(32),e.CREATE_PARAMS),
  20116.         IS_NULLABLE     = t.allownulls,
  20117.         CASE_SENSITIVE      = d.case_sensitive,
  20118.         SEARCHABLE      = d.searchable,
  20119.         UNSIGNED_ATTRIBUTE  = d.unsigned_attribute,
  20120.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  20121.         AUTO_UNIQUE_VALUE   = d.auto_unique_value, 
  20122.         LOCAL_TYPE_NAME     = case  when t.usertype = 80 then t.name 
  20123.                         else d.local_type_name
  20124.                         end,
  20125.         MINIMUM_SCALE       = convert(smallint,
  20126.                         case 
  20127.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  20128.                         else null 
  20129.                         end),
  20130.         MAXIMUM_SCALE       = convert(smallint,
  20131.                         case 
  20132.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  20133.                         then @@max_precision
  20134.                         else null 
  20135.                         end),
  20136.         GUID            = convert(binary(16),null),
  20137.         TYPELIB         = convert(varchar(1),null),
  20138.         VERSION         = convert(varchar(1),null),
  20139.         IS_LONG         = d.is_long,
  20140.         BEST_MATCH      = case when t.usertype = 80
  20141.                         then convert(bit,0)
  20142.                         else d.best_match
  20143.                         end,
  20144.         IS_FIXEDLENGTH      = convert(bit, 
  20145.                         case when d.fixlen is null then 0 else 1 end)
  20146.     from    master.dbo.spt_provider_types d, master.dbo.spt_datatype_info_ext e, systypes t
  20147.     where   d.ss_dtype = t.type 
  20148.     and t.usertype <= 100
  20149.     and t.usertype <> 18 /* sysname */
  20150.     and (case when t.usertype = 80 /* TIMESTAMP */ then 1 else 0 end 
  20151.             = case when d.type_name = 'timestamp' then 1 else 0 end)
  20152.     and     t.usertype *= e.user_type 
  20153.     and     e.AUTO_INCREMENT = 0
  20154.     and     t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  20155.     and (@data_type is null or d.oledb_data_type = @data_type)
  20156.     and (@best_match is null or d.best_match = @best_match)
  20157.     order by 2
  20158. go
  20159. dump tran master with no_log
  20160. go
  20161.  
  20162. if (charindex('7.00', @@version) = 0 and
  20163.     charindex('8.00', @@version) = 0)
  20164. begin
  20165.     print ''
  20166.     print ''
  20167.     print 'Warning:'
  20168.     print 'you are installing the stored procedures '
  20169.     print 'on a pre 8.0 SQL Server.'
  20170.     print 'Ignore the following errors.'
  20171. end
  20172. else
  20173.     drop proc sp_provider_types_rowset
  20174. go
  20175.  
  20176. /*  Procedure for 8.0 server */
  20177. create proc sp_provider_types_rowset
  20178.     (
  20179.     @data_type  smallint = null,
  20180.     @best_match tinyint  = null
  20181.     )
  20182. as
  20183.     select
  20184.         TYPE_NAME       = case  when t.usertype = 80 then t.name 
  20185.                         else d.type_name 
  20186.                         end,
  20187.         DATA_TYPE       = d.oledb_data_type,                            
  20188.  
  20189.  
  20190.         COLUMN_SIZE     = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  20191.                             then @@max_precision
  20192.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  20193.         LITERAL_PREFIX      = d.literal_prefix,
  20194.         LITERAL_SUFFIX      = d.literal_suffix,
  20195.         CREATE_PARAMS       = convert(nvarchar(32),e.CREATE_PARAMS),
  20196.         IS_NULLABLE     = t.allownulls,
  20197.         CASE_SENSITIVE      = d.case_sensitive,
  20198.         SEARCHABLE      = d.searchable,
  20199.         UNSIGNED_ATTRIBUTE  = d.unsigned_attribute,
  20200.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  20201.         AUTO_UNIQUE_VALUE   = d.auto_unique_value, 
  20202.         LOCAL_TYPE_NAME     = case  when t.usertype = 80 then t.name 
  20203.                         else d.local_type_name
  20204.                         end,
  20205.         MINIMUM_SCALE       = convert(smallint,
  20206.                         case 
  20207.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  20208.                         else null 
  20209.                         end),
  20210.         MAXIMUM_SCALE       = convert(smallint,
  20211.                         case 
  20212.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  20213.                         then @@max_precision
  20214.                         else null 
  20215.                         end),
  20216.         GUID            = convert(uniqueidentifier,null),
  20217.         TYPELIB         = convert(nvarchar(1),null),
  20218.         VERSION         = convert(nvarchar(1),null),
  20219.         IS_LONG         = d.is_long,
  20220.         BEST_MATCH      = case  when t.usertype = 80
  20221.                         then convert(bit,0)
  20222.                         else d.best_match
  20223.                         end,
  20224.         IS_FIXEDLENGTH      = convert(bit, 
  20225.                         case when d.fixlen is null then 0 else 1 end)
  20226.  
  20227.     from master.dbo.spt_provider_types d
  20228.         INNER JOIN master.dbo.systypes t on d.ss_dtype = t.xtype
  20229.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  20230.             t.xusertype = e.user_type
  20231.             and e.AUTO_INCREMENT = 0
  20232.     where   (@data_type is null or d.oledb_data_type = @data_type)
  20233.     and (@best_match is null or d.best_match = @best_match) 
  20234.     and t.usertype <= 255
  20235.     and t.usertype <> 18 /* sysname */
  20236.     order by 2
  20237. go
  20238.  
  20239. grant execute on sp_provider_types_rowset to public
  20240. go
  20241.  
  20242. dump tran master with no_log
  20243. go
  20244. if (charindex('6.00', @@version) > 0)
  20245.     begin
  20246.     if (exists (select * from sysobjects
  20247.         where name = 'sp_provider_types_rowset' and type = 'P '))
  20248.         begin
  20249.         drop procedure sp_provider_types_rowset
  20250.         dump tran master with no_log
  20251.         end
  20252.     end
  20253. go
  20254.  
  20255.  
  20256. print ''
  20257. print 'creating sp_procedure_params_rowset'
  20258. go
  20259.  
  20260. /*  Procedure for 6.0 and 6.50 servers */
  20261. create procedure sp_procedure_params_rowset
  20262.     (
  20263.     @procedure_name     varchar(255) = null,
  20264.     @group_number       int = null,
  20265.     @procedure_schema   varchar(255) = null,
  20266.     @parameter_name     varchar(255) = null
  20267.     )
  20268. as
  20269. IF @procedure_name is not null
  20270.     BEGIN
  20271.     select
  20272.         PROCEDURE_CATALOG   = db_name(),
  20273.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20274.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20275.         PARAMETER_NAME      = c.name,
  20276.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20277.         PARAMETER_TYPE      = convert(smallint, 1+((c.status/64)&1)),
  20278.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20279.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20280.         IS_NULLABLE     = convert(bit,
  20281.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20282.                         then 0 else 1
  20283.                         end),
  20284.         DATA_TYPE       = d.oledb_data_type,
  20285.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20286.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20287.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20288.                         then coalesce(d.column_size,c.length)
  20289.                         else null 
  20290.                         end),
  20291.         CHARACTER_OCTET_LENGTH  = convert(int,
  20292.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20293.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20294.                         then coalesce(d.column_size,c.length)
  20295.                         else null 
  20296.                         end),
  20297.         NUMERIC_PRECISION   = convert(smallint,
  20298.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec 
  20299.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20300.                             then d.data_precision else null end),
  20301.         NUMERIC_SCALE       = convert(smallint, 
  20302.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20303.         DESCRIPTION     = convert(varchar(1),null),
  20304.         TYPE_NAME       = d.type_name,
  20305.         LOCAL_TYPE_NAME     = d.local_type_name
  20306.     
  20307.     from
  20308.         syscolumns c,
  20309.         sysobjects o,
  20310.         master.dbo.spt_provider_types d,
  20311.         systypes t
  20312.     where
  20313.         o.name = @procedure_name
  20314.     and o.type = 'P'                            /* Just Procedures */
  20315.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20316.     and o.id = c.id
  20317.     and c.number = @group_number
  20318.     and c.type = d.ss_dtype
  20319.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20320.     and c.usertype = t.usertype
  20321.     and (t.usertype != 80 or d.type_name='timestamp')
  20322.     and (@parameter_name is null or @parameter_name = c.name)
  20323.     UNION ALL
  20324.     SELECT         /* return value row*/
  20325.         PROCEDURE_CATALOG   = db_name(),
  20326.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20327.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20328.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  20329.         ORDINAL_POSITION    = convert(smallint,0),
  20330.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20331.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20332.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20333.         IS_NULLABLE     = convert(bit,0),
  20334.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  20335.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20336.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  20337.         NUMERIC_PRECISION   = convert(smallint,10),
  20338.         NUMERIC_SCALE       = convert(smallint,null),
  20339.         DESCRIPTION     = convert(varchar(1),null),
  20340.         TYPE_NAME       = convert(sysname,'int'),
  20341.         LOCAL_TYPE_NAME     = convert(sysname,'int')
  20342.     from
  20343.         syscomments c,
  20344.         sysobjects o
  20345.     where
  20346.         o.name = @procedure_name
  20347.     and o.type = 'P'                        /* Just Procedures */
  20348.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20349.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20350.     and c.id = o.id
  20351.     and c.number = @group_number
  20352.     and c.colid = 1
  20353.     order by 2, 3, 5
  20354.     END
  20355. ELSE
  20356.     BEGIN
  20357.     select
  20358.         PROCEDURE_CATALOG   = db_name(),
  20359.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20360.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20361.         PARAMETER_NAME      = c.name,
  20362.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20363.         PARAMETER_TYPE      = convert(smallint, 1+((c.status/64)&1)),
  20364.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20365.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20366.         IS_NULLABLE     = convert(bit,
  20367.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20368.                         then 0 else 1
  20369.                         end),
  20370.         DATA_TYPE       = d.oledb_data_type,
  20371.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20372.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20373.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20374.                         then coalesce(d.column_size,c.length)
  20375.                         else null 
  20376.                         end),
  20377.         CHARACTER_OCTET_LENGTH  = convert(int,
  20378.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20379.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20380.                         then coalesce(d.column_size,c.length)
  20381.                         else null 
  20382.                         end),
  20383.         NUMERIC_PRECISION   = convert(smallint,
  20384.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20385.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20386.                             then d.data_precision else null end),
  20387.         NUMERIC_SCALE       = convert(smallint, 
  20388.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20389.         DESCRIPTION     = convert(varchar(1),null),
  20390.         TYPE_NAME       = d.type_name,
  20391.         LOCAL_TYPE_NAME     = d.local_type_name
  20392.     
  20393.     from
  20394.         syscolumns c,
  20395.         sysobjects o,
  20396.         master.dbo.spt_provider_types d,
  20397.         systypes t
  20398.     where
  20399.         o.type = 'P'                            /* Just Procedures */
  20400.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20401.     and o.id = c.id
  20402.     and c.type = d.ss_dtype
  20403.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20404.     and c.usertype = t.usertype
  20405.     and (t.usertype != 80 or d.type_name='timestamp')
  20406.     and (@parameter_name is null or @parameter_name = c.name)
  20407.     UNION ALL
  20408.     SELECT         /* return value row*/
  20409.         PROCEDURE_CATALOG   = db_name(),
  20410.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20411.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20412.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  20413.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20414.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20415.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20416.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20417.         IS_NULLABLE     = convert(bit,0),
  20418.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  20419.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20420.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  20421.         NUMERIC_PRECISION   = convert(smallint,10),
  20422.         NUMERIC_SCALE       = convert(smallint,null),
  20423.         DESCRIPTION     = convert(varchar(1),null),
  20424.         TYPE_NAME       = convert(sysname,'int'),
  20425.         LOCAL_TYPE_NAME     = convert(sysname,'int')
  20426.     from
  20427.         syscomments c,
  20428.         sysobjects o
  20429.     where
  20430.         o.type = 'P'                        /* Just Procedures */
  20431.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20432.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20433.     and c.id = o.id
  20434.     and     c.colid = 1
  20435.     order by 2, 3, 5
  20436.     END
  20437. go
  20438. dump tran master with no_log
  20439. go
  20440. create procedure sp_procedure_params_rowset;2
  20441.     (
  20442.     @handle         int output,
  20443.     @scrollopt      int output,
  20444.     @ccopt          int output,
  20445.     @rows           int output,
  20446.     @procedure_name     varchar(255) = null,
  20447.     @group_number       int = null,
  20448.     @procedure_schema   varchar(255) = null,
  20449.     @parameter_name     varchar(255) = null
  20450.     )
  20451. as
  20452.     declare @ret int
  20453.  
  20454.  
  20455. SET NOCOUNT ON
  20456.  
  20457.     create table #spprocparamrowset1
  20458.         (
  20459.         PROCEDURE_CATALOG   sysname not null,
  20460.         PROCEDURE_SCHEMA    sysname not null,
  20461.         PROCEDURE_NAME      varchar(35) not null,
  20462.         PARAMETER_NAME      sysname not null,
  20463.         ORDINAL_POSITION    smallint not null,
  20464.         PARAMETER_TYPE      smallint null,
  20465.         PARAMETER_HASDEFAULT    tinyint null,
  20466.         PARAMETER_DEFAULT   varchar(255) null,
  20467.         IS_NULLABLE     bit not null,
  20468.         DATA_TYPE       smallint null,
  20469.         CHARACTER_MAXIMUM_LENGTH int null,
  20470.         CHARACTER_OCTET_LENGTH  int null,
  20471.         NUMERIC_PRECISION   smallint null,
  20472.         NUMERIC_SCALE       smallint null,
  20473.         DESCRIPTION     varchar(1) null,
  20474.         TYPE_NAME       sysname null,
  20475.         LOCAL_TYPE_NAME     sysname null,
  20476.         )
  20477.  
  20478. IF @procedure_name is not null
  20479.     BEGIN
  20480.     insert into #spprocparamrowset1
  20481.     select
  20482.         PROCEDURE_CATALOG   = db_name(),
  20483.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20484.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20485.         PARAMETER_NAME      = c.name,
  20486.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20487.         PARAMETER_TYPE      = convert(smallint, 1+((c.status/64)&1)),
  20488.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20489.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20490.         IS_NULLABLE     = convert(bit,
  20491.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20492.                         then 0 else 1
  20493.                         end),
  20494.         DATA_TYPE       = d.oledb_data_type,
  20495.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20496.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20497.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20498.                         then coalesce(d.column_size,c.length)
  20499.                         else null 
  20500.                         end),
  20501.         CHARACTER_OCTET_LENGTH  = convert(int,
  20502.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20503.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20504.                         then coalesce(d.column_size,c.length)
  20505.                         else null 
  20506.                         end),
  20507.         NUMERIC_PRECISION   = convert(smallint,
  20508.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20509.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20510.                             then d.data_precision else null end),
  20511.         NUMERIC_SCALE       = convert(smallint, 
  20512.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20513.         DESCRIPTION     = convert(varchar(1),null),
  20514.         TYPE_NAME       = d.type_name,
  20515.         LOCAL_TYPE_NAME     = d.local_type_name
  20516.     
  20517.     from
  20518.         syscolumns c,
  20519.         sysobjects o,
  20520.         master.dbo.spt_provider_types d,
  20521.         systypes t
  20522.     where
  20523.         o.name = @procedure_name
  20524.     and o.type = 'P'                            /* Just Procedures */
  20525.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20526.     and o.id = c.id
  20527.     and c.number = @group_number
  20528.     and c.type = d.ss_dtype
  20529.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20530.     and c.usertype = t.usertype
  20531.     and (t.usertype != 80 or d.type_name='timestamp')
  20532.     and (@parameter_name is null or @parameter_name = c.name)
  20533.     UNION ALL
  20534.     SELECT         /* return value row*/
  20535.         PROCEDURE_CATALOG   = db_name(),
  20536.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20537.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20538.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  20539.         ORDINAL_POSITION    = convert(smallint,0),
  20540.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20541.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20542.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20543.         IS_NULLABLE     = convert(bit,0),
  20544.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  20545.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20546.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  20547.         NUMERIC_PRECISION   = convert(smallint,10),
  20548.         NUMERIC_SCALE       = convert(smallint,null),
  20549.         DESCRIPTION     = convert(varchar(1),null),
  20550.         TYPE_NAME       = convert(sysname,'int'),
  20551.         LOCAL_TYPE_NAME     = convert(sysname,'int')
  20552.     from
  20553.         syscomments c,
  20554.         sysobjects o
  20555.     where
  20556.         o.name = @procedure_name
  20557.     and o.type = 'P'                        /* Just Procedures */
  20558.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20559.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20560.     and c.id = o.id
  20561.     and c.number = @group_number
  20562.     and c.colid = 1
  20563.     order by 2, 3, 5
  20564.     END
  20565. ELSE
  20566.     BEGIN
  20567.     insert into #spprocparamrowset1
  20568.     select
  20569.         PROCEDURE_CATALOG   = db_name(),
  20570.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20571.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20572.         PARAMETER_NAME      = c.name,
  20573.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20574.         PARAMETER_TYPE      = convert(smallint, 1+((c.status/64)&1)),
  20575.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20576.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20577.         IS_NULLABLE     = convert(bit,
  20578.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20579.                         then 0 else 1
  20580.                         end),
  20581.         DATA_TYPE       = d.oledb_data_type,
  20582.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20583.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20584.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20585.                         then coalesce(d.column_size,c.length)
  20586.                         else null 
  20587.                         end),
  20588.         CHARACTER_OCTET_LENGTH  = convert(int,
  20589.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20590.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20591.                         then coalesce(d.column_size,c.length)
  20592.                         else null 
  20593.                         end),
  20594.         NUMERIC_PRECISION   = convert(smallint,
  20595.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20596.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20597.                             then d.data_precision else null end),
  20598.         NUMERIC_SCALE       = convert(smallint, 
  20599.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20600.         DESCRIPTION     = convert(varchar(1),null),
  20601.         TYPE_NAME       = d.type_name,
  20602.         LOCAL_TYPE_NAME     = d.local_type_name
  20603.     
  20604.     from
  20605.         syscolumns c,
  20606.         sysobjects o,
  20607.         master.dbo.spt_provider_types d,
  20608.         systypes t
  20609.     where
  20610.         o.type = 'P'                            /* Just Procedures */
  20611.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20612.     and o.id = c.id
  20613.     and c.type = d.ss_dtype
  20614.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20615.     and c.usertype = t.usertype
  20616.     and (t.usertype != 80 or d.type_name='timestamp')
  20617.     and (@parameter_name is null or @parameter_name = c.name)
  20618.     UNION ALL
  20619.     SELECT         /* return value row*/
  20620.         PROCEDURE_CATALOG   = db_name(),
  20621.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20622.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20623.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  20624.         ORDINAL_POSITION    = convert(smallint,0),
  20625.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20626.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20627.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20628.         IS_NULLABLE     = convert(bit,0),
  20629.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  20630.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20631.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  20632.         NUMERIC_PRECISION   = convert(smallint,10),
  20633.         NUMERIC_SCALE       = convert(smallint,null),
  20634.         DESCRIPTION     = convert(varchar(1),null),
  20635.         TYPE_NAME       = convert(sysname,'int'),
  20636.         LOCAL_TYPE_NAME     = convert(sysname,'int')
  20637.     from
  20638.         syscomments c,
  20639.         sysobjects o
  20640.     where
  20641.         o.type = 'P'                        /* Just Procedures */
  20642.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20643.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20644.     and c.id = o.id
  20645.     and     c.colid = 1
  20646.     order by 2, 3, 5
  20647.     END
  20648.  
  20649.     exec @ret = sp_cursoropen @handle output, 'select * from #spprocparamrowset1',
  20650.         @scrollopt output, @ccopt output, @rows output
  20651.  
  20652.     drop table #spprocparamrowset1
  20653.     return isnull(@ret,0)
  20654. go
  20655. dump tran master with no_log
  20656. go
  20657. create procedure sp_procedure_params_rowset;3
  20658. as
  20659.     select
  20660.         PROCEDURE_CATALOG   = convert(sysname, ' '),
  20661.         PROCEDURE_SCHEMA    = convert(sysname, ' '),
  20662.         PROCEDURE_NAME      = convert(varchar(35), ' '),
  20663.         PARAMETER_NAME      = convert(sysname, ' '),
  20664.         ORDINAL_POSITION    = convert(smallint, 0),
  20665.         PARAMETER_TYPE      = convert(smallint, 0),
  20666.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20667.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20668.         IS_NULLABLE     = convert(bit, 0),
  20669.         DATA_TYPE       = convert(smallint, 0),
  20670.         CHARACTER_MAXIMUM_LENGTH= convert(int, 0),
  20671.         CHARACTER_OCTET_LENGTH  = convert(int, 0),
  20672.         NUMERIC_PRECISION   = convert(smallint, 0),
  20673.         NUMERIC_SCALE       = convert(smallint, 0),
  20674.         DESCRIPTION     = convert(varchar(1),null),
  20675.         TYPE_NAME       = convert(sysname,null),
  20676.         LOCAL_TYPE_NAME     = convert(sysname,null)
  20677.     where   1=0
  20678. go
  20679. dump tran master with no_log
  20680. go
  20681.  
  20682.  
  20683. if (charindex('7.00', @@version) > 0 or
  20684.     charindex('8.00', @@version) > 0)
  20685.     drop procedure sp_procedure_params_rowset
  20686. else
  20687. begin
  20688.     print ''
  20689.     print ''
  20690.     print 'Warning:'
  20691.     print 'you are installing the stored procedures '
  20692.     print 'on a pre 7.0 SQL Server.'
  20693.     print 'Ignore the following errors.'
  20694. end
  20695. go
  20696.  
  20697. /*  Procedure for 7.0 servers */
  20698. create procedure sp_procedure_params_rowset
  20699.     (
  20700.     @procedure_name     sysname,
  20701.     @group_number       int = 1,
  20702.     @procedure_schema   sysname = null,
  20703.     @parameter_name     sysname = null
  20704.     )
  20705. as
  20706.     select
  20707.         PROCEDURE_CATALOG   = db_name(),
  20708.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20709.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20710.         PARAMETER_NAME      = c.name,
  20711.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20712.         PARAMETER_TYPE      = convert(smallint, 1+c.isoutparam),
  20713.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20714.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  20715.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20716.         DATA_TYPE       = d.oledb_data_type,
  20717.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20718.                         case 
  20719.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20720.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20721.                         then coalesce(d.column_size,c.length)
  20722.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20723.                         then coalesce(d.column_size,c.length/2)
  20724.                         else null 
  20725.                         end),
  20726.         CHARACTER_OCTET_LENGTH  = convert(int,
  20727.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20728.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20729.                         then coalesce(d.column_size,c.length)
  20730.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20731.                         then coalesce(d.column_size*2,c.length)
  20732.                         else null 
  20733.                         end),
  20734.         NUMERIC_PRECISION   = convert(smallint,
  20735.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20736.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20737.                             then d.data_precision else null end),
  20738.         NUMERIC_SCALE       = convert(smallint, 
  20739.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20740.         DESCRIPTION     = convert(nvarchar(1),null),
  20741.         TYPE_NAME       = d.type_name,
  20742.         LOCAL_TYPE_NAME     = d.local_type_name
  20743.     
  20744.     from
  20745.         sysobjects o,
  20746.         syscolumns c,
  20747.         master.dbo.spt_provider_types d,
  20748.         systypes t
  20749.     where
  20750.         o.name = @procedure_name
  20751.     and o.type = 'P'                            /* Just Procedures */
  20752.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20753.     and o.id = c.id
  20754.     and c.number = @group_number
  20755.     and c.xtype = d.ss_dtype
  20756.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20757.     and c.xusertype = t.xusertype
  20758.     and (@parameter_name is null or @parameter_name = c.name)
  20759.     UNION ALL
  20760.     SELECT         /* return value row*/
  20761.         PROCEDURE_CATALOG   = db_name(),
  20762.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20763.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20764.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  20765.         ORDINAL_POSITION    = convert(smallint,0),
  20766.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20767.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20768.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  20769.         IS_NULLABLE     = convert(bit,0),
  20770.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  20771.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20772.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  20773.         NUMERIC_PRECISION   = convert(smallint,10),
  20774.         NUMERIC_SCALE       = convert(smallint,null),
  20775.         DESCRIPTION     = convert(nvarchar(1),null),
  20776.         TYPE_NAME       = convert(sysname,N'int'),
  20777.         LOCAL_TYPE_NAME     = convert(sysname,N'int')
  20778.     from
  20779.         sysobjects o,
  20780.         syscomments c
  20781.     where
  20782.         o.name = @procedure_name
  20783.     and o.id = c.id 
  20784.     and c.number = @group_number
  20785.     and c.colid = 1
  20786.     and o.type = 'P'                        /* Just Procedures */
  20787.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20788.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20789.     order by 2, 3, 5
  20790. go
  20791. dump tran master with no_log
  20792. go
  20793. create procedure sp_procedure_params_rowset;2
  20794.     (
  20795.     @procedure_schema   sysname = null,
  20796.     @parameter_name     sysname = null
  20797.     )
  20798. as
  20799.     select
  20800.         PROCEDURE_CATALOG   = db_name(),
  20801.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20802.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20803.         PARAMETER_NAME      = c.name,
  20804.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20805.         PARAMETER_TYPE      = convert(smallint, 1+c.isoutparam),
  20806.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20807.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  20808.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20809.         DATA_TYPE       = d.oledb_data_type,
  20810.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20811.                         case 
  20812.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20813.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20814.                         then coalesce(d.column_size,c.length)
  20815.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20816.                         then coalesce(d.column_size,c.length/2)
  20817.                         else null 
  20818.                         end),
  20819.         CHARACTER_OCTET_LENGTH  = convert(int,
  20820.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20821.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20822.                         then coalesce(d.column_size,c.length)
  20823.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20824.                         then coalesce(d.column_size*2,c.length)
  20825.                         else null 
  20826.                         end),
  20827.         NUMERIC_PRECISION   = convert(smallint,
  20828.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20829.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20830.                             then d.data_precision else null end),
  20831.         NUMERIC_SCALE       = convert(smallint, 
  20832.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20833.         DESCRIPTION     = convert(nvarchar(1),null),
  20834.         TYPE_NAME       = d.type_name,
  20835.         LOCAL_TYPE_NAME     = d.local_type_name
  20836.     
  20837.     from
  20838.         sysobjects o,
  20839.         syscolumns c,
  20840.         master.dbo.spt_provider_types d,
  20841.         systypes t
  20842.     where
  20843.         o.type = 'P'                            /* Just Procedures */
  20844.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20845.     and o.id = c.id
  20846.     and c.xtype = d.ss_dtype
  20847.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20848.     and c.xusertype = t.xusertype
  20849.     and (@parameter_name is null or @parameter_name = c.name)
  20850.     UNION ALL
  20851.     SELECT         /* return value row*/
  20852.         PROCEDURE_CATALOG   = db_name(),
  20853.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20854.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20855.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  20856.         ORDINAL_POSITION    = convert(smallint,0),
  20857.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20858.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20859.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  20860.         IS_NULLABLE     = convert(bit,0),
  20861.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  20862.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20863.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  20864.         NUMERIC_PRECISION   = convert(smallint,10),
  20865.         NUMERIC_SCALE       = convert(smallint,null),
  20866.         DESCRIPTION     = convert(nvarchar(1),null),
  20867.         TYPE_NAME       = convert(sysname,N'int'),
  20868.         LOCAL_TYPE_NAME     = convert(sysname,N'int')
  20869.     from
  20870.         sysobjects o,
  20871.         syscomments c
  20872.     where
  20873.         o.type = 'P'        /* Just Procedures */
  20874.     and o.id = c.id 
  20875.     and c.colid = 1
  20876.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20877.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20878.     order by 2, 3, 5
  20879. go
  20880.  
  20881. if (charindex('8.00', @@version) > 0)
  20882.     drop procedure sp_procedure_params_rowset
  20883. else
  20884. begin
  20885.     print ''
  20886.     print ''
  20887.     print 'Warning:'
  20888.     print 'you are installing the stored procedures '
  20889.     print 'on a pre 8.0 SQL Server.'
  20890.     print 'Ignore the following errors.'
  20891. end
  20892. go
  20893.  
  20894. /*  Procedure for 8.0 servers */
  20895. create procedure sp_procedure_params_rowset
  20896.     (
  20897.     @procedure_name     sysname,
  20898.     @group_number       int = 1,
  20899.     @procedure_schema   sysname = null,
  20900.     @parameter_name     sysname = null
  20901.     )
  20902. as
  20903.     select
  20904.         PROCEDURE_CATALOG   = db_name(),
  20905.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20906.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20907.         PARAMETER_NAME      = c.name,
  20908.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20909.         PARAMETER_TYPE      = convert(smallint, 1+c.isoutparam),
  20910.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20911.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  20912.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20913.         DATA_TYPE       = d.oledb_data_type,
  20914.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20915.                         case 
  20916.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20917.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20918.                         then coalesce(d.column_size,c.length)
  20919.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20920.                         then coalesce(d.column_size,c.length/2)
  20921.                         else null 
  20922.                         end),
  20923.         CHARACTER_OCTET_LENGTH  = convert(int,
  20924.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20925.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20926.                         then coalesce(d.column_size,c.length)
  20927.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20928.                         then coalesce(d.column_size*2,c.length)
  20929.                         else null 
  20930.                         end),
  20931.         NUMERIC_PRECISION   = convert(smallint,
  20932.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20933.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20934.                             then d.data_precision else null end),
  20935.         NUMERIC_SCALE       = convert(smallint, 
  20936.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20937.         DESCRIPTION     = convert(nvarchar(1),null),
  20938.         TYPE_NAME       = d.type_name,
  20939.         LOCAL_TYPE_NAME     = d.local_type_name
  20940.     
  20941.     from
  20942.         sysobjects o,
  20943.         syscolumns c,
  20944.         master.dbo.spt_provider_types d,
  20945.         systypes t
  20946.     where
  20947.         o.name = @procedure_name
  20948.     and     (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  20949.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20950.     and o.id = c.id
  20951.     and ((c.number = @group_number and o.type = 'P') 
  20952.             or (c.number = 0 and o.type = 'FN')
  20953.             or (c.number = 1 and o.type in ('TF', 'IF')))
  20954.     and c.xtype = d.ss_dtype
  20955.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20956.     and c.xusertype = t.xusertype
  20957.     and (@parameter_name is null or @parameter_name = c.name)
  20958.     UNION ALL
  20959.     SELECT         /* return value row*/
  20960.         PROCEDURE_CATALOG   = db_name(),
  20961.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20962.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20963.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  20964.         ORDINAL_POSITION    = convert(smallint,0),
  20965.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20966.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20967.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  20968.         IS_NULLABLE     = convert(bit,0),
  20969.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  20970.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20971.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  20972.         NUMERIC_PRECISION   = convert(smallint,10),
  20973.         NUMERIC_SCALE       = convert(smallint,null),
  20974.         DESCRIPTION     = convert(nvarchar(1),null),
  20975.         TYPE_NAME       = convert(sysname,N'int'),
  20976.         LOCAL_TYPE_NAME     = convert(sysname,N'int')
  20977.     from
  20978.         sysobjects o,
  20979.         syscomments c
  20980.     where
  20981.         o.name = @procedure_name
  20982.     and o.id = c.id 
  20983.     and c.number = @group_number
  20984.     and c.colid = 1
  20985.     and o.type = 'P'            /* Just Procedures */
  20986.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20987.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20988.     UNION ALL
  20989.     SELECT         /* UDF return value row*/
  20990.         PROCEDURE_CATALOG        = db_name(),
  20991.         PROCEDURE_SCHEMA         = user_name(o.uid),
  20992.         PROCEDURE_NAME           = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20993.         PARAMETER_NAME           = convert(sysname,'@RETURN_VALUE'),
  20994.         ORDINAL_POSITION         = convert(smallint, 0),
  20995.         PARAMETER_TYPE           = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20996.         PARAMETER_HASDEFAULT     = convert(tinyint, 0),
  20997.         PARAMETER_DEFAULT        = convert(nvarchar(255),null),
  20998.         IS_NULLABLE              = convert(bit, c.isnullable),
  20999.         DATA_TYPE                = d.oledb_data_type,
  21000.         CHARACTER_MAXIMUM_LENGTH = convert(int,
  21001.                         case 
  21002.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21003.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21004.                         then coalesce(d.column_size,c.length)
  21005.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21006.                         then coalesce(d.column_size,c.length/2)
  21007.                         else null 
  21008.                         end),
  21009.         CHARACTER_OCTET_LENGTH   = convert(int,
  21010.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21011.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21012.                         then coalesce(d.column_size,c.length)
  21013.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21014.                         then coalesce(d.column_size*2,c.length)
  21015.                         else null 
  21016.                         end),
  21017.         NUMERIC_PRECISION        = convert(smallint,
  21018.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  21019.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  21020.                             then d.data_precision else null end),
  21021.         NUMERIC_SCALE            = convert(smallint, 
  21022.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  21023.         DESCRIPTION              = convert(nvarchar(1),null),
  21024.         TYPE_NAME                = d.type_name,
  21025.         LOCAL_TYPE_NAME          = d.local_type_name
  21026.     FROM
  21027.         sysobjects o,
  21028.         syscolumns c,
  21029.         master.dbo.spt_provider_types d,
  21030.         systypes t
  21031.     WHERE
  21032.         o.name = @procedure_name
  21033.     and o.id = c.id 
  21034.     and c.number = 0
  21035.     and c.colid = 0
  21036.     and o.type = 'FN'           /* UDF scalar functions */
  21037.     and c.xtype = d.ss_dtype
  21038.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  21039.     and c.xusertype = t.xusertype
  21040.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21041.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  21042.     UNION ALL
  21043.     SELECT         /* UDF table value row*/
  21044.         PROCEDURE_CATALOG   = db_name(),
  21045.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21046.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  21047.         PARAMETER_NAME      = convert(sysname,'@TABLE_RETURN_VALUE'),
  21048.         ORDINAL_POSITION    = convert(smallint, 0),
  21049.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  21050.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  21051.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  21052.         IS_NULLABLE     = convert(bit,0),
  21053.         DATA_TYPE       = convert(smallint, 0),             /*DBTYPE_EMPTY*/
  21054.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  21055.                         case 
  21056.                         when d.oledb_data_type = 129        /*DBTYPE_STR*/ 
  21057.                             or d.oledb_data_type = 128  /*DBTYPE_BYTES*/
  21058.                         then coalesce(d.column_size,c.length)
  21059.                         when d.oledb_data_type = 130        /*DBTYPE_WSTR*/
  21060.                         then coalesce(d.column_size,c.length/2)
  21061.                         else null 
  21062.                         end),
  21063.         CHARACTER_OCTET_LENGTH  = convert(int,
  21064.                         case when d.oledb_data_type = 129   /*DBTYPE_STR*/ 
  21065.                             or d.oledb_data_type = 128  /*DBTYPE_BYTES*/
  21066.                         then coalesce(d.column_size,c.length)
  21067.                         when d.oledb_data_type = 130        /*DBTYPE_WSTR*/
  21068.                         then coalesce(d.column_size*2,c.length)
  21069.                         else null 
  21070.                         end),
  21071.         NUMERIC_PRECISION   = convert(smallint,
  21072.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  21073.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  21074.                             then d.data_precision else null end),
  21075.         NUMERIC_SCALE       = convert(smallint, 
  21076.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  21077.         DESCRIPTION     = convert(nvarchar(50), N'Result table returned by table valued function'),
  21078.         TYPE_NAME       = N'table',
  21079.         LOCAL_TYPE_NAME     = N'table'
  21080.     
  21081.     from
  21082.         sysobjects o,
  21083.         syscolumns c,
  21084.         master.dbo.spt_provider_types d
  21085.     where
  21086.         o.name = @procedure_name
  21087.     and o.id = c.id 
  21088.     and c.number = 0
  21089.     and c.colid = 1
  21090.     and o.type in ('TF', 'IF')          /* UDF table functions */
  21091.     and c.xtype = d.ss_dtype
  21092.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  21093.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21094.     and (@parameter_name is null or @parameter_name = '@TABLE_RETURN_VALUE')
  21095.     order by 2, 3, 5
  21096. go
  21097. dump tran master with no_log
  21098. go
  21099. create procedure sp_procedure_params_rowset;2
  21100.     (
  21101.     @procedure_schema   sysname = null,
  21102.     @parameter_name     sysname = null
  21103.     )
  21104. as
  21105.     select
  21106.         PROCEDURE_CATALOG   = db_name(),
  21107.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21108.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  21109.         PARAMETER_NAME      = c.name,
  21110.         ORDINAL_POSITION    = convert(smallint, c.colid),
  21111.         PARAMETER_TYPE      = convert(smallint, 1+c.isoutparam),
  21112.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  21113.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  21114.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  21115.         DATA_TYPE       = d.oledb_data_type,
  21116.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  21117.                         case 
  21118.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21119.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21120.                         then coalesce(d.column_size,c.length)
  21121.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21122.                         then coalesce(d.column_size,c.length/2)
  21123.                         else null 
  21124.                         end),
  21125.         CHARACTER_OCTET_LENGTH  = convert(int,
  21126.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21127.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21128.                         then coalesce(d.column_size,c.length)
  21129.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21130.                         then coalesce(d.column_size*2,c.length)
  21131.                         else null 
  21132.                         end),
  21133.         NUMERIC_PRECISION   = convert(smallint,
  21134.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  21135.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  21136.                             then d.data_precision else null end),
  21137.         NUMERIC_SCALE       = convert(smallint, 
  21138.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  21139.         DESCRIPTION     = convert(nvarchar(1),null),
  21140.         TYPE_NAME       = d.type_name,
  21141.         LOCAL_TYPE_NAME     = d.local_type_name
  21142.     
  21143.     from
  21144.         sysobjects o,
  21145.         syscolumns c,
  21146.         master.dbo.spt_provider_types d,
  21147.         systypes t
  21148.     where
  21149.         (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  21150.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21151.     and o.id = c.id
  21152.     and (o.type = 'P' or (c.number = 0 and o.type = 'FN') or (c.number = 1 and o.type in ('TF', 'IF')))
  21153.     and c.xtype = d.ss_dtype
  21154.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  21155.     and c.xusertype = t.xusertype
  21156.     and (@parameter_name is null or @parameter_name = c.name)
  21157.     UNION ALL
  21158.     SELECT         /* return value row*/
  21159.         PROCEDURE_CATALOG   = db_name(),
  21160.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21161.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  21162.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  21163.         ORDINAL_POSITION    = convert(smallint,0),
  21164.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  21165.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  21166.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  21167.         IS_NULLABLE     = convert(bit,0),
  21168.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  21169.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  21170.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  21171.         NUMERIC_PRECISION   = convert(smallint,10),
  21172.         NUMERIC_SCALE       = convert(smallint,null),
  21173.         DESCRIPTION     = convert(nvarchar(1),null),
  21174.         TYPE_NAME       = convert(sysname,N'int'),
  21175.         LOCAL_TYPE_NAME     = convert(sysname,N'int')
  21176.     from
  21177.         sysobjects o,
  21178.         syscomments c
  21179.     where
  21180.         o.type = 'P'            /* Just Procedures */
  21181.     and o.id = c.id 
  21182.     and c.colid = 1
  21183.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21184.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  21185.     UNION ALL
  21186.     SELECT         /* UDF return value row*/
  21187.         PROCEDURE_CATALOG   = db_name(),
  21188.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21189.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  21190.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  21191.         ORDINAL_POSITION    = convert(smallint, 0),
  21192.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  21193.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  21194.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  21195.         IS_NULLABLE     = convert(bit, c.isnullable),
  21196.         DATA_TYPE       = d.oledb_data_type,
  21197.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  21198.                         case 
  21199.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21200.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21201.                         then coalesce(d.column_size,c.length)
  21202.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21203.                         then coalesce(d.column_size,c.length/2)
  21204.                         else null 
  21205.                         end),
  21206.         CHARACTER_OCTET_LENGTH  = convert(int,
  21207.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21208.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21209.                         then coalesce(d.column_size,c.length)
  21210.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21211.                         then coalesce(d.column_size*2,c.length)
  21212.                         else null 
  21213.                         end),
  21214.         NUMERIC_PRECISION   = convert(smallint,
  21215.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  21216.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  21217.                             then d.data_precision else null end),
  21218.         NUMERIC_SCALE       = convert(smallint, 
  21219.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  21220.         DESCRIPTION     = convert(nvarchar(1),null),
  21221.         TYPE_NAME       = d.type_name,
  21222.         LOCAL_TYPE_NAME     = d.local_type_name
  21223.     
  21224.     from
  21225.         sysobjects o,
  21226.         syscolumns c,
  21227.         master.dbo.spt_provider_types d,
  21228.         systypes t
  21229.     where
  21230.         o.id = c.id 
  21231.     and c.number = 0
  21232.     and c.colid = 0
  21233.     and o.type = 'FN'           /* UDF scalar functions */
  21234.     and c.xtype = d.ss_dtype
  21235.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  21236.     and c.xusertype = t.xusertype
  21237.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21238.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  21239.     UNION ALL
  21240.     SELECT         /* UDF table value row*/
  21241.         PROCEDURE_CATALOG   = db_name(),
  21242.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21243.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  21244.         PARAMETER_NAME      = convert(sysname,'@TABLE_RETURN_VALUE'),
  21245.         ORDINAL_POSITION    = convert(smallint, 0),
  21246.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  21247.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  21248.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  21249.         IS_NULLABLE     = convert(bit,0),
  21250.         DATA_TYPE       = convert(smallint, 0),         /*DBTYPE_EMPTY*/
  21251.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  21252.                         case 
  21253.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21254.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21255.                         then coalesce(d.column_size,c.length)
  21256.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21257.                         then coalesce(d.column_size,c.length/2)
  21258.                         else null 
  21259.                         end),
  21260.         CHARACTER_OCTET_LENGTH  = convert(int,
  21261.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21262.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21263.                         then coalesce(d.column_size,c.length)
  21264.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21265.                         then coalesce(d.column_size*2,c.length)
  21266.                         else null 
  21267.                         end),
  21268.         NUMERIC_PRECISION   = convert(smallint,
  21269.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  21270.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  21271.                             then d.data_precision else null end),
  21272.         NUMERIC_SCALE       = convert(smallint, 
  21273.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  21274.         DESCRIPTION     = convert(nvarchar(1),null),
  21275.         TYPE_NAME       = N'table',
  21276.         LOCAL_TYPE_NAME     = N'table'
  21277.     
  21278.     from
  21279.         sysobjects o,
  21280.         syscolumns c,
  21281.         master.dbo.spt_provider_types d
  21282.     where
  21283.         o.id = c.id 
  21284.     and c.number = 0
  21285.     and c.colid = 1
  21286.     and o.type in ('TF', 'IF')          /* UDF table functions */
  21287.     and c.xtype = d.ss_dtype
  21288.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  21289.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21290.     and (@parameter_name is null or @parameter_name = '@TABLE_RETURN_VALUE')
  21291.     order by 2, 3, 5
  21292. go
  21293.  
  21294. grant execute on sp_procedure_params_rowset to public
  21295. go
  21296.  
  21297. dump tran master with no_log
  21298. go
  21299. if (charindex('6.00', @@version) > 0)
  21300.     begin
  21301.     if (exists (select * from sysobjects
  21302.             where name = 'sp_procedure_params_rowset' and type = 'P '))
  21303.         begin
  21304.         drop procedure sp_procedure_params_rowset
  21305.         dump tran master with no_log
  21306.         end
  21307.     end
  21308. go
  21309.  
  21310.  
  21311. print ''
  21312. print 'creating sp_procedures_rowset'
  21313. go
  21314.  
  21315.  
  21316. /* pre 7.0 version */
  21317. create procedure sp_procedures_rowset
  21318.     (
  21319.     @procedure_name     varchar(255), 
  21320.     @group_number       int = 1,
  21321.     @procedure_schema   varchar(255) = null
  21322.     )       
  21323. as
  21324.     select
  21325.         PROCEDURE_CATALOG   = db_name(),
  21326.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21327.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  21328.         PROCEDURE_TYPE      = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21329.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  21330.         DESCRIPTION     = convert(varchar(1),null),
  21331.         DATE_CREATED        = o.crdate,
  21332.         DATE_MODIFIED       = convert(datetime,null)
  21333.     from    
  21334.         sysobjects o, 
  21335.         syscomments p,
  21336.         sysusers u
  21337.     where
  21338.             o.name = @procedure_name
  21339.         and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21340.     and     o.type = 'P'        /* Object type of Procedure */
  21341.     and     p.colid = 1
  21342.     and     p.id = o.id
  21343.     and p.number = @group_number
  21344.     and     u.uid = user_id()   /* constrain sysusers uid for use in subquery */
  21345.     and     (   suser_id() = 1  /* User is the System Administrator */
  21346.         or  o.uid = user_id()   /* User created the object */
  21347.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21348.         or  ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21349.              from sysprotects p
  21350.              where p.id = o.id
  21351.                  /*  get rows for public,current user,user's group */
  21352.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21353.                  /* check for SELECT,EXECUTE privilege */
  21354.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  21355.             ) = 1    /* final magic...compare Grants    */
  21356.         )
  21357.     order by 2, 3
  21358. go
  21359. dump tran master with no_log
  21360. go
  21361. create procedure sp_procedures_rowset;2
  21362.     (
  21363.     @procedure_schema   varchar(255) = null
  21364.     )       
  21365. as
  21366.     select
  21367.         PROCEDURE_CATALOG   = db_name(),
  21368.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21369.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  21370.         PROCEDURE_TYPE      = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21371.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  21372.         DESCRIPTION     = convert(varchar(1),null),
  21373.         DATE_CREATED        = o.crdate,
  21374.         DATE_MODIFIED       = convert(datetime,null)
  21375.     from    
  21376.         sysobjects o, 
  21377.         syscomments p,
  21378.         sysusers u
  21379.     where
  21380.             (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21381.     and     o.type = 'P'        /* Object type of Procedure */
  21382.     and     p.colid = 1
  21383.     and     p.id = o.id
  21384.     and     u.uid = user_id()   /* constrain sysusers uid for use in subquery */
  21385.     and     (   suser_id() = 1  /* User is the System Administrator */
  21386.         or  o.uid = user_id()   /* User created the object */
  21387.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21388.         or  ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21389.              from sysprotects p
  21390.              where p.id = o.id
  21391.                  /*  get rows for public,current user,user's group */
  21392.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21393.                  /* check for SELECT,EXECUTE privilege */
  21394.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  21395.             ) = 1    /* final magic...compare Grants    */
  21396.         )
  21397.     order by 2, 3
  21398. go
  21399. dump tran master with no_log
  21400. go
  21401.  
  21402. if (charindex('7.00', @@version) > 0 or
  21403.     charindex('8.00', @@version) > 0)
  21404.     drop procedure sp_procedures_rowset
  21405. else
  21406. begin
  21407.     print ''
  21408.     print ''
  21409.     print 'Warning:'
  21410.     print 'you are installing the stored procedures '
  21411.     print 'on a pre 7.0 SQL Server.'
  21412.     print 'Ignore the following errors.'
  21413. end
  21414. go
  21415.  
  21416. /* 7.0 version */
  21417. create procedure sp_procedures_rowset
  21418.     (
  21419.     @procedure_name     sysname, 
  21420.     @group_number       int = 1,
  21421.     @procedure_schema   sysname = null
  21422.     )       
  21423. as
  21424.     select
  21425.         PROCEDURE_CATALOG   = db_name(),
  21426.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21427.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21428.         PROCEDURE_TYPE      = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21429.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21430.         DESCRIPTION     = convert(nvarchar(1),null),
  21431.         DATE_CREATED        = o.crdate,
  21432.         DATE_MODIFIED       = convert(datetime,null)
  21433.     from    
  21434.         sysobjects o, 
  21435.         syscomments p
  21436.     where
  21437.         permissions(o.id) <> 0
  21438.     and o.name = @procedure_name
  21439.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21440.     and     o.type = 'P'        /* Object type of Procedure */
  21441.     and     p.colid = 1
  21442.     and     p.id = o.id
  21443.     and p.number = @group_number
  21444.     order by 2, 3
  21445. go
  21446. dump tran master with no_log
  21447. go
  21448. create procedure sp_procedures_rowset;2
  21449.     (
  21450.     @procedure_schema   sysname = null
  21451.     )       
  21452. as
  21453.     select
  21454.         PROCEDURE_CATALOG   = db_name(),
  21455.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21456.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21457.         PROCEDURE_TYPE      = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21458.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21459.         DESCRIPTION     = convert(nvarchar(1),null),
  21460.         DATE_CREATED        = o.crdate,
  21461.         DATE_MODIFIED       = convert(datetime,null)
  21462.     from    sysobjects o, syscomments p
  21463.     where
  21464.         permissions(o.id) <> 0
  21465.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21466.     and     o.type = 'P'        /* Object type of Procedure */
  21467.     and p.colid = 1
  21468.     and p.id = o.id
  21469.     order by 2, 3
  21470. go
  21471.  
  21472. if (charindex('8.00', @@version) > 0)
  21473.     drop procedure sp_procedures_rowset
  21474. else
  21475. begin
  21476.     print ''
  21477.     print ''
  21478.     print 'Warning:'
  21479.     print 'you are installing the stored procedures '
  21480.     print 'on a pre 8.0 SQL Server.'
  21481.     print 'Ignore the following errors.'
  21482. end
  21483. go
  21484.  
  21485. /* 8.0 version */
  21486. create procedure sp_procedures_rowset
  21487.     (
  21488.     @procedure_name     sysname, 
  21489.     @group_number       int = 1,
  21490.     @procedure_schema   sysname = null
  21491.     )       
  21492. as
  21493.     select
  21494.         PROCEDURE_CATALOG   = db_name(),
  21495.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21496.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21497.         PROCEDURE_TYPE      = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21498.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21499.         DESCRIPTION     = convert(nvarchar(1),null),
  21500.         DATE_CREATED        = o.crdate,
  21501.         DATE_MODIFIED       = convert(datetime,null)
  21502.     from    
  21503.         sysobjects o, 
  21504.         syscomments p
  21505.     where
  21506.         permissions(o.id) <> 0
  21507.     and o.name = @procedure_name
  21508.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21509.     and     o.type in ('P', 'FN', 'TF', 'IF')   /* Object type of Procedure */
  21510.     and     p.colid = 1
  21511.     and     p.id = o.id
  21512.     and p.number = @group_number
  21513.     order by 2, 3
  21514. go
  21515. dump tran master with no_log
  21516. go
  21517. create procedure sp_procedures_rowset;2
  21518.     (
  21519.     @procedure_schema   sysname = null
  21520.     )       
  21521. as
  21522.     select
  21523.         PROCEDURE_CATALOG   = db_name(),
  21524.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21525.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21526.         PROCEDURE_TYPE      = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21527.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21528.         DESCRIPTION     = convert(nvarchar(1),null),
  21529.         DATE_CREATED        = o.crdate,
  21530.         DATE_MODIFIED       = convert(datetime,null)
  21531.     from    sysobjects o, syscomments p
  21532.     where
  21533.         permissions(o.id) <> 0
  21534.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21535.     and     o.type in ('P', 'FN', 'TF', 'IF')   /* Object type of Procedure */
  21536.     and p.colid = 1
  21537.     and p.id = o.id
  21538.     order by 2, 3
  21539. go
  21540.  
  21541. grant execute on sp_procedures_rowset to public
  21542. go
  21543.  
  21544. dump tran master with no_log
  21545. go
  21546.  
  21547. if (charindex('6.00', @@version) > 0)
  21548.     begin
  21549.     if (exists (select * from sysobjects
  21550.             where name = 'sp_procedures_rowset' and type = 'P '))
  21551.         begin
  21552.         drop procedure sp_procedures_rowset
  21553.         dump tran master with no_log
  21554.         end
  21555.     end
  21556. go
  21557.  
  21558.  
  21559. print ''
  21560. print 'creating sp_schemata_rowset'
  21561. go
  21562.  
  21563. /*  6.0 and 6.5 and 7.0 version */
  21564. create procedure sp_schemata_rowset
  21565.     (
  21566.     @schema_name    varchar(90) = null, 
  21567.     @schema_owner   varchar(90) = null
  21568.     )       
  21569. as
  21570.     select  distinct
  21571.         CATALOG_NAME            = db_name(),
  21572.         SCHEMA_NAME         = user_name(o.uid), 
  21573.         SCHEMA_OWNER            = user_name(o.uid), 
  21574.         DEFAULT_CHARACTER_SET_CATALOG   = convert(sysname,'master'),
  21575.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,'dbo'),
  21576.         DEFAULT_CHARACTER_SET_NAME  = convert(sysname,a_cha.name)
  21577.     from    
  21578.         sysobjects o,
  21579.         master.dbo.sysconfigures    cfg,
  21580.         master.dbo.syscharsets      a_cha, /* charset/1001, not sortorder */
  21581.         master.dbo.syscharsets      b_cha  /* sortorder/2001, not charset */
  21582.     where   
  21583.         (@schema_name is null or @schema_name = user_name(o.uid))
  21584.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  21585.     and a_cha.type = 1001 /* type is charset */
  21586.     and     b_cha.type = 2001 /* type is sortorder */
  21587.     and     a_cha.id = b_cha.csid
  21588.     and     b_cha.id = cfg.value
  21589.     order by 2
  21590. go
  21591. dump tran master with no_log
  21592. go
  21593.  
  21594. if (charindex('8.00', @@version) > 0)
  21595.     drop procedure sp_schemata_rowset
  21596. else
  21597. begin
  21598.     print ''
  21599.     print ''
  21600.     print 'Warning:'
  21601.     print 'you are installing the stored procedures '
  21602.     print 'on a pre 8.0 SQL Server.'
  21603.     print 'Ignore the following errors.'
  21604. end
  21605. go
  21606.  
  21607. /* 8.0 version */
  21608. create procedure sp_schemata_rowset
  21609.     (
  21610.     @schema_name    sysname = null, 
  21611.     @schema_owner   sysname = null
  21612.     )       
  21613. as
  21614.     select  distinct
  21615.         CATALOG_NAME            = db_name(),
  21616.         SCHEMA_NAME         = user_name(o.uid), 
  21617.         SCHEMA_OWNER            = user_name(o.uid), 
  21618.         DEFAULT_CHARACTER_SET_CATALOG   = convert(sysname,N'master'),
  21619.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,N'dbo'),
  21620.         DEFAULT_CHARACTER_SET_NAME  = convert(sysname,a_cha.name)
  21621.     from    
  21622.         sysobjects o,
  21623.         master.dbo.syscharsets      a_cha /* charset/1001, not sortorder */
  21624.     where   
  21625.         (@schema_name is null or @schema_name = user_name(o.uid))
  21626.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  21627.     and a_cha.type = 1001 /* type is charset */
  21628.     and     a_cha.id = convert(tinyint, DatabasePropertyEx(db_name(), 'sqlcharset')) /* what is charset of a table? */
  21629.     order by 2
  21630. go
  21631.  
  21632. dump tran master with no_log
  21633. go
  21634.  
  21635. /* The following stored procedure is used for Sphinx and Hydra */
  21636. create procedure sp_schemata_rowset;3
  21637. as
  21638.     select
  21639.         CATALOG_NAME            = convert(sysname,' '),
  21640.         SCHEMA_NAME         = convert(sysname,' '), 
  21641.         SCHEMA_OWNER            = convert(sysname,' '), 
  21642.         DEFAULT_CHARACTER_SET_CATALOG   = convert(sysname,' '),
  21643.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,' '),
  21644.         DEFAULT_CHARACTER_SET_NAME  = convert(sysname,' ')
  21645.     where   1=0
  21646. go
  21647. grant execute on sp_schemata_rowset to public
  21648. go
  21649.  
  21650. dump tran master with no_log
  21651. go
  21652. if (charindex('6.00', @@version) > 0)
  21653.     begin
  21654.     if (exists (select * from sysobjects
  21655.             where name = 'sp_schemata_rowset' and type = 'P '))
  21656.         begin
  21657.         drop procedure sp_schemata_rowset
  21658.         dump tran master with no_log
  21659.         end
  21660.     end
  21661. go
  21662.  
  21663.  
  21664. print ''
  21665. print 'creating sp_statistics_rowset'
  21666. go
  21667.  
  21668.  
  21669. /*  6.0 and 6.5 version */
  21670. create procedure sp_statistics_rowset
  21671.     (
  21672.     @table_name varchar(255),
  21673.     @table_schema   varchar(255) = null  
  21674.     )
  21675. as
  21676.     select  db_name()               as TABLE_CATALOG,       
  21677.         user_name(o.uid)            as TABLE_SCHEMA,
  21678.         o.name                  as TABLE_NAME,
  21679.         x.rows                  as CARDINALITY
  21680.     from    sysobjects o, sysindexes x, sysusers u
  21681.     where   o.type in ('U')
  21682.     and     o.name = @table_name
  21683.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21684.     and     x.id = o.id
  21685.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  21686.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21687.     and     (
  21688.         suser_id() = 1   /* User is the System Administrator */
  21689.         or o.uid = user_id()     /* User created the object */
  21690.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21691.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21692.             from sysprotects p
  21693.             where p.id = o.id
  21694.             /* get rows for public,current user,user's group */
  21695.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21696.             /* check for SELECT,EXECUTE privilege */
  21697.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21698.             ) = 1   /* final magic...compare Grants   */
  21699.            )
  21700.     order by 2, 3
  21701. go
  21702. dump tran master with no_log
  21703. go
  21704. create procedure sp_statistics_rowset;2
  21705.     (
  21706.     @table_schema   varchar(255) = null  
  21707.     )
  21708. as
  21709.     select  db_name()               as TABLE_CATALOG,       
  21710.         user_name(o.uid)            as TABLE_SCHEMA,
  21711.         o.name                  as TABLE_NAME,
  21712.         x.rows                  as CARDINALITY
  21713.     from    sysobjects o, sysindexes x, sysusers u
  21714.     where   o.type in ('U')
  21715.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21716.     and     x.id = o.id
  21717.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21718.     and     (
  21719.         suser_id() = 1   /* User is the System Administrator */
  21720.         or o.uid = user_id()     /* User created the object */
  21721.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21722.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21723.             from sysprotects p
  21724.             where p.id = o.id
  21725.             /* get rows for public,current user,user's group */
  21726.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21727.             /* check for SELECT,EXECUTE privilege */
  21728.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21729.             ) = 1   /* final magic...compare Grants   */
  21730.            )        
  21731.     order by 2, 3
  21732. go
  21733. dump tran master with no_log
  21734. go
  21735.  
  21736. if (charindex('7.00', @@version) > 0 or
  21737.     charindex('8.00', @@version) > 0)
  21738.     drop procedure sp_statistics_rowset
  21739. else
  21740. begin
  21741.     print ''
  21742.     print ''
  21743.     print 'Warning:'
  21744.     print 'you are installing the stored procedures '
  21745.     print 'on a pre 8.0 SQL Server.'
  21746.     print 'Ignore the following errors.'
  21747. end
  21748. go
  21749.  
  21750. /*  8.0 version */
  21751. create procedure sp_statistics_rowset
  21752.     (
  21753.     @table_name sysname,
  21754.     @table_schema   sysname = null   
  21755.     )
  21756. as
  21757.     select  db_name()               as TABLE_CATALOG,       
  21758.         user_name(o.uid)            as TABLE_SCHEMA,
  21759.         o.name                  as TABLE_NAME,
  21760.         x.rows                  as CARDINALITY
  21761.     from    sysobjects o, sysindexes x
  21762.     where   o.type in ('U')
  21763.     and     o.name = @table_name
  21764.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21765.     and     x.id = o.id
  21766.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  21767.     and     permissions(o.id) <> 0
  21768.     order by 2, 3
  21769. go
  21770. dump tran master with no_log
  21771. go
  21772. create procedure sp_statistics_rowset;2
  21773.     (
  21774.     @table_schema   sysname = null   
  21775.     )
  21776. as
  21777.     select  db_name()               as TABLE_CATALOG,       
  21778.         user_name(o.uid)            as TABLE_SCHEMA,
  21779.         o.name                  as TABLE_NAME,
  21780.         x.rows                  as CARDINALITY
  21781.     from    sysobjects o, sysindexes x
  21782.     where   o.type in ('U')
  21783.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21784.     and     x.id = o.id
  21785.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  21786.     and     permissions(o.id) <> 0
  21787.     order by 2, 3
  21788. go
  21789.  
  21790. grant execute on sp_statistics_rowset to public
  21791. go
  21792.  
  21793. dump tran master with no_log
  21794. go
  21795. if (charindex('6.00', @@version) > 0)
  21796.     begin
  21797.     if (exists (select * from sysobjects
  21798.             where name = 'sp_statistics_rowset' and type = 'P '))
  21799.         begin
  21800.         drop procedure sp_statistics_rowset
  21801.         dump tran master with no_log
  21802.         end
  21803.     end
  21804. go
  21805.  
  21806.  
  21807. print ''
  21808. print 'creating sp_tables_rowset'
  21809. go
  21810.  
  21811.  
  21812. /*  Procedure for 6.50 and earlier servers */
  21813. create procedure sp_tables_rowset
  21814.     (
  21815.     @table_name varchar(255), 
  21816.     @table_schema   varchar(255) = null,    
  21817.     @table_type varchar(255) = null 
  21818.     )
  21819. as
  21820.     select  TABLE_CATALOG   = db_name(),
  21821.         TABLE_SCHEMA    = user_name(o.uid),
  21822.         TABLE_NAME  = o.name,
  21823.         TABLE_TYPE  = convert(varchar(30),
  21824.                     case o.type 
  21825.                     when 'U' then 'TABLE'
  21826.                     when 'V' then 'VIEW'
  21827.                     when 'S' then 'SYSTEM TABLE'
  21828.                     end),
  21829.         TABLE_GUID  = convert(binary(16), null),
  21830.         DESCRIPTION = convert(varchar(1), null),
  21831.         TABLE_PROPID    = convert(int,null),
  21832.         DATE_CREATED    = o.crdate,
  21833.         DATE_MODIFIED   = convert(datetime,null)
  21834.     from    sysusers u, sysobjects o
  21835.     where   o.type in ('U','V','S')
  21836.     and     o.name = @table_name
  21837.     and     (   @table_schema is null
  21838.         or  @table_schema = user_name(o.uid)
  21839.         )
  21840.     and     (
  21841.             @table_type is null
  21842.         or  @table_type = case o.type 
  21843.                     when 'U' then 'TABLE'
  21844.                     when 'V' then 'VIEW'
  21845.                     when 'S' then 'SYSTEM TABLE'
  21846.                     end
  21847.         )
  21848.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21849.     and     (
  21850.         suser_id() = 1   /* User is the System Administrator */
  21851.         or o.uid = user_id()     /* User created the object */
  21852.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21853.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21854.             from sysprotects p
  21855.             /* join to correlate with all rows in sysobjects */
  21856.             where p.id = o.id
  21857.             /* get rows for public,current user,user's group */
  21858.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21859.             /* check for SELECT,EXECUTE privilege */
  21860.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21861.             ) = 1   /* final magic...compare Grants   */
  21862.         )       
  21863.     order by 4, 2, 3
  21864. go
  21865. dump tran master with no_log
  21866. go
  21867. create procedure sp_tables_rowset;2
  21868.     (
  21869.     @table_schema   varchar(255) = null,    
  21870.     @table_type varchar(255) = null 
  21871.     )
  21872. as
  21873.     select  TABLE_CATALOG   = db_name(),
  21874.         TABLE_SCHEMA    = user_name(o.uid),
  21875.         TABLE_NAME  = o.name,
  21876.         TABLE_TYPE  = convert(varchar(30),
  21877.                     case o.type 
  21878.                     when 'U' then 'TABLE'
  21879.                     when 'V' then 'VIEW'
  21880.                     when 'S' then 'SYSTEM TABLE'
  21881.                     end),
  21882.         TABLE_GUID  = convert(binary(16), null),
  21883.         DESCRIPTION = convert(varchar(1), null),
  21884.         TABLE_PROPID    = convert(int,null),
  21885.         DATE_CREATED    = o.crdate,
  21886.         DATE_MODIFIED   = convert(datetime,null)
  21887.     from    sysusers u, sysobjects o
  21888.     where   o.type in ('U','V','S')
  21889.     and     (   @table_schema is null
  21890.         or  @table_schema = user_name(o.uid)
  21891.         )
  21892.     and     (
  21893.             @table_type is null
  21894.         or  @table_type = case o.type 
  21895.                     when 'U' then 'TABLE'
  21896.                     when 'V' then 'VIEW'
  21897.                     when 'S' then 'SYSTEM TABLE'
  21898.                     end
  21899.         )
  21900.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21901.     and     (
  21902.         suser_id() = 1   /* User is the System Administrator */
  21903.         or o.uid = user_id()     /* User created the object */
  21904.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21905.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21906.             from sysprotects p
  21907.             /* join to correlate with all rows in sysobjects */
  21908.             where p.id = o.id
  21909.             /* get rows for public,current user,user's group */
  21910.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21911.             /* check for SELECT,EXECUTE privilege */
  21912.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21913.             ) = 1   /* final magic...compare Grants   */
  21914.         )       
  21915.     order by 4, 2, 3
  21916. go
  21917. dump tran master with no_log
  21918. go
  21919.  
  21920. if (charindex('7.00', @@version) > 0 or
  21921.     charindex('8.00', @@version) > 0)
  21922.     drop procedure sp_tables_rowset
  21923. else
  21924. begin
  21925.     print ''
  21926.     print ''
  21927.     print 'Warning:'
  21928.     print 'you are installing the stored procedures '
  21929.     print 'on a pre 8.0 SQL Server.'
  21930.     print 'Ignore the following errors.'
  21931. end
  21932. go
  21933.  
  21934. /*  Procedure for 8.0 server */
  21935. create procedure sp_tables_rowset
  21936.     (
  21937.     @table_name sysname, 
  21938.     @table_schema   sysname = null, 
  21939.     @table_type nvarchar(255) = null 
  21940.     )
  21941. as
  21942.     select  *
  21943.     from    (select TABLE_CATALOG   = db_name(),
  21944.             TABLE_SCHEMA    = user_name(uid),
  21945.             TABLE_NAME  = name,
  21946.             TABLE_TYPE  = convert(nvarchar(30),
  21947.                         case type 
  21948.                         when 'U' then 
  21949.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21950.                             then N'TABLE' else N'SYSTEM TABLE' end
  21951.                         when 'S' then N'SYSTEM TABLE'
  21952.                         when 'V' then 
  21953.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21954.                             then N'VIEW' else N'SYSTEM VIEW' end
  21955.                         end),
  21956.             TABLE_GUID  = convert(uniqueidentifier, null),
  21957.             DESCRIPTION = convert(nvarchar(1), null),
  21958.             TABLE_PROPID    = convert(int,null),
  21959.             DATE_CREATED    = crdate,
  21960.             DATE_MODIFIED   = convert(datetime,null)
  21961.         from    sysobjects
  21962.         where   name = @table_name
  21963.         and type in ('U','V','S')
  21964.         and permissions(id) <> 0
  21965.         ) as o
  21966.  
  21967.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  21968.     and     (@table_type is null or @table_type = TABLE_TYPE)   
  21969.     order by 4, 2, 3
  21970. go
  21971. dump tran master with no_log
  21972. go
  21973. create procedure sp_tables_rowset;2
  21974.     (
  21975.     @table_schema   sysname = null, 
  21976.     @table_type nvarchar(255) = null 
  21977.     )
  21978. as
  21979.     select  *
  21980.     from    (select TABLE_CATALOG   = db_name(),
  21981.             TABLE_SCHEMA    = user_name(uid),
  21982.             TABLE_NAME  = name,
  21983.             TABLE_TYPE  = convert(nvarchar(30),
  21984.                         case type 
  21985.                         when 'U' then 
  21986.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21987.                             then N'TABLE' else N'SYSTEM TABLE' end
  21988.                         when 'S' then N'SYSTEM TABLE'
  21989.                         when 'V' then 
  21990.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21991.                             then N'VIEW' else N'SYSTEM VIEW' end
  21992.                         end),
  21993.             TABLE_GUID  = convert(uniqueidentifier, null),
  21994.             DESCRIPTION = convert(nvarchar(1), null),
  21995.             TABLE_PROPID    = convert(int,null),
  21996.             DATE_CREATED    = crdate,
  21997.             DATE_MODIFIED   = convert(datetime,null)
  21998.         from    sysobjects
  21999.         where   type in ('U','V','S')
  22000.         and permissions(id) <> 0
  22001.         ) as o
  22002.  
  22003.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22004.     and     (@table_type is null or @table_type = TABLE_TYPE)   
  22005.     order by 4, 2, 3
  22006. go
  22007. dump tran master with no_log
  22008. go
  22009. create procedure sp_tables_rowset;5
  22010.     (
  22011.     @table_server       sysname,
  22012.     @table_catalog      sysname = null,
  22013.     @table_name     sysname = null,
  22014.     @table_schema       sysname = null,
  22015.     @table_type     sysname = null
  22016.     )
  22017. as
  22018.     select
  22019.         TABLE_CATALOG,
  22020.         TABLE_SCHEMA,
  22021.         TABLE_NAME,
  22022.         TABLE_TYPE,
  22023.         TABLE_GUID,
  22024.         DESCRIPTION
  22025.     --  TABLE_PROPID,
  22026.     --  DATE_CREATED,
  22027.     --  DATE_MODIFIED
  22028.     from master.dbo.SYSREMOTE_TABLES <
  22029.                 @table_server,
  22030.                 @table_catalog,
  22031.                 @table_schema,
  22032.                 @table_name,
  22033.                 @table_type >
  22034.     order by 4,1,2,3
  22035. go
  22036.  
  22037. grant execute on sp_tables_rowset to public
  22038. go
  22039.  
  22040. dump tran master with no_log
  22041. go
  22042. if (charindex('6.00', @@version) > 0)
  22043.     begin
  22044.     if (exists (select * from sysobjects
  22045.             where name = 'sp_tables_rowset' and type = 'P '))
  22046.         begin
  22047.         drop procedure sp_tables_rowset
  22048.         dump tran master with no_log
  22049.         end
  22050.     end
  22051. go
  22052.  
  22053.  
  22054.  
  22055. print ''
  22056. print 'creating sp_tables_info_rowset'
  22057. go
  22058.  
  22059.  
  22060. /*  Procedure for 6.50 and earlier servers */
  22061. create procedure sp_tables_info_rowset
  22062.     (
  22063.     @table_name varchar(255), 
  22064.     @table_schema   varchar(255) = null,    
  22065.     @table_type varchar(255) = null 
  22066.     )
  22067. as
  22068.     select  TABLE_CATALOG       = db_name(),
  22069.         TABLE_SCHEMA        = user_name(o.uid),
  22070.         TABLE_NAME      = o.name,
  22071.         TABLE_TYPE      = convert(varchar(30),
  22072.                         case o.type 
  22073.                         when 'U' then 'TABLE'
  22074.                         when 'V' then 'VIEW'
  22075.                         when 'S' then 'SYSTEM TABLE'
  22076.                         end),
  22077.         TABLE_GUID      = convert(binary(16), null),
  22078.         BOOKMARKS       = convert(bit, 1),
  22079.         BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22080.         BOOKMARK_DATATYPE   = convert(smallint, 19 /*DBTYPE_UI4 */),
  22081.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22082.         BOOKMARK_INFORMATION    = convert(int, 0),
  22083.         TABLE_VERSION       = convert(int, o.schema_ver),
  22084.         CARDINALITY     = x.rows,
  22085.         DESCRIPTION     = convert(varchar(1), null),
  22086.         TABLE_PROPID        = convert(int, null)
  22087.  
  22088.     from    sysusers u, 
  22089.         sysobjects o,
  22090.         sysindexes x
  22091.  
  22092.     where   o.type in ('U','V','S')
  22093.     and     o.name = @table_name
  22094.     and     (   @table_schema is null
  22095.         or  @table_schema = user_name(o.uid)
  22096.         )
  22097.     and     (
  22098.             @table_type is null
  22099.         or  @table_type = case o.type 
  22100.                     when 'U' then 'TABLE'
  22101.                     when 'V' then 'VIEW'
  22102.                     when 'S' then 'SYSTEM TABLE'
  22103.                     end
  22104.         )
  22105.     and o.id *= x.id
  22106.     and x.indid in (0,1)
  22107.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  22108.     and     (
  22109.         suser_id() = 1   /* User is the System Administrator */
  22110.         or o.uid = user_id()     /* User created the object */
  22111.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  22112.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  22113.             from sysprotects p
  22114.             /* join to correlate with all rows in sysobjects */
  22115.             where p.id = o.id
  22116.             /* get rows for public,current user,user's group */
  22117.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  22118.             /* check for SELECT,EXECUTE privilege */
  22119.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  22120.             ) = 1   /* final magic...compare Grants   */
  22121.         )       
  22122.     order by 4, 2, 3
  22123. go
  22124. dump tran master with no_log
  22125. go
  22126. create procedure sp_tables_info_rowset;2
  22127.     (
  22128.     @table_schema   varchar(255) = null,    
  22129.     @table_type varchar(255) = null 
  22130.     )
  22131. as
  22132.     select  TABLE_CATALOG       = db_name(),
  22133.         TABLE_SCHEMA        = user_name(o.uid),
  22134.         TABLE_NAME      = o.name,
  22135.         TABLE_TYPE      = convert(varchar(30),
  22136.                         case o.type 
  22137.                         when 'U' then 'TABLE'
  22138.                         when 'V' then 'VIEW'
  22139.                         when 'S' then 'SYSTEM TABLE'
  22140.                         end),
  22141.         TABLE_GUID      = convert(binary(16), null),
  22142.         BOOKMARKS       = convert(bit, 1),
  22143.         BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22144.         BOOKMARK_DATATYPE   = convert(smallint, 19 /*DBTYPE_UI4 */),
  22145.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22146.         BOOKMARK_INFORMATION    = convert(int, 0),
  22147.         TABLE_VERSION       = convert(int, o.schema_ver),
  22148.         CARDINALITY     = x.rows,
  22149.         DESCRIPTION     = convert(varchar(1), null),
  22150.         TABLE_PROPID        = convert(int, null)
  22151.  
  22152.     from    sysusers u, 
  22153.         sysobjects o,
  22154.         sysindexes x
  22155.  
  22156.     where   o.type in ('U','V','S')
  22157.     and     (   @table_schema is null
  22158.         or  @table_schema = user_name(o.uid)
  22159.         )
  22160.     and     (
  22161.             @table_type is null
  22162.         or  @table_type = case o.type 
  22163.                     when 'U' then 'TABLE'
  22164.                     when 'V' then 'VIEW'
  22165.                     when 'S' then 'SYSTEM TABLE'
  22166.                     end
  22167.         )
  22168.     and o.id *= x.id
  22169.     and x.indid in (0,1)
  22170.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  22171.     and     (
  22172.         suser_id() = 1   /* User is the System Administrator */
  22173.         or o.uid = user_id()     /* User created the object */
  22174.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  22175.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  22176.             from sysprotects p
  22177.             /* join to correlate with all rows in sysobjects */
  22178.             where p.id = o.id
  22179.             /* get rows for public,current user,user's group */
  22180.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  22181.             /* check for SELECT,EXECUTE privilege */
  22182.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  22183.             ) = 1   /* final magic...compare Grants   */
  22184.         )       
  22185.     order by 4, 2, 3
  22186. go
  22187. dump tran master with no_log
  22188. go
  22189.  
  22190. if (charindex('7.00', @@version) > 0 or
  22191.     charindex('8.00', @@version) > 0)
  22192.     drop procedure sp_tables_info_rowset
  22193. else
  22194. begin
  22195.     print ''
  22196.     print ''
  22197.     print 'Warning:'
  22198.     print 'you are installing the stored procedures '
  22199.     print 'on a pre 7.0 SQL Server.'
  22200.     print 'Ignore the following errors.'
  22201. end
  22202. go
  22203.  
  22204. /*  Procedure for 7.0 server */
  22205. create procedure sp_tables_info_rowset
  22206.     (
  22207.     @table_name sysname, 
  22208.     @table_schema   sysname = null, 
  22209.     @table_type nvarchar(255) = null 
  22210.     )
  22211. as
  22212.     select  *
  22213.     from    (select TABLE_CATALOG       = db_name(),
  22214.             TABLE_SCHEMA        = user_name(o.uid),
  22215.             TABLE_NAME      = o.name,
  22216.             TABLE_TYPE      = convert(nvarchar(30),
  22217.                             case o.type 
  22218.                             when 'U' then 
  22219.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22220.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22221.                             when 'S' then N'SYSTEM TABLE'
  22222.                             when 'V' then 
  22223.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22224.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22225.                             end),
  22226.             TABLE_GUID      = convert(uniqueidentifier, null),
  22227.             BOOKMARKS       = convert(bit, 1),
  22228.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22229.             BOOKMARK_DATATYPE   = convert(smallint, 19 /*DBTYPE_UI4 */),
  22230.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22231.             BOOKMARK_INFORMATION    = convert(int, 0),
  22232.             TABLE_VERSION       = convert(int, o.schema_ver),
  22233.             CARDINALITY     = x.rows,
  22234.             DESCRIPTION     = convert(nvarchar(1), null),
  22235.             TABLE_PROPID        = convert(int, null) 
  22236.     
  22237.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22238.         where   o.name = @table_name
  22239.         and     o.type in ('U','V','S')
  22240.         and permissions(o.id) <> 0) as t
  22241.  
  22242.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22243.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22244.     order by 4, 2, 3
  22245. go
  22246. dump tran master with no_log
  22247. go
  22248. create procedure sp_tables_info_rowset;2
  22249.     (
  22250.     @table_schema   sysname = null, 
  22251.     @table_type nvarchar(255) = null 
  22252.     )
  22253. as
  22254.     select  *
  22255.     from    (select TABLE_CATALOG       = db_name(),
  22256.             TABLE_SCHEMA        = user_name(o.uid),
  22257.             TABLE_NAME      = o.name,
  22258.             TABLE_TYPE      = convert(nvarchar(30),
  22259.                             case o.type 
  22260.                             when 'U' then 
  22261.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22262.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22263.                             when 'S' then N'SYSTEM TABLE'
  22264.                             when 'V' then 
  22265.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22266.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22267.                             end),
  22268.             TABLE_GUID      = convert(uniqueidentifier, null),
  22269.             BOOKMARKS       = convert(bit, 1),
  22270.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22271.             BOOKMARK_DATATYPE   = convert(smallint, 19 /*DBTYPE_UI4 */),
  22272.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22273.             BOOKMARK_INFORMATION    = convert(int, 0),
  22274.             TABLE_VERSION       = convert(int, o.schema_ver),
  22275.             CARDINALITY     = x.rows,
  22276.             DESCRIPTION     = convert(nvarchar(1), null),
  22277.             TABLE_PROPID        = convert(int, null) 
  22278.     
  22279.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22280.         where   o.type in ('U','V','S')
  22281.         and permissions(o.id) <> 0) as t
  22282.  
  22283.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22284.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22285.     order by 4, 2, 3
  22286. go
  22287.  
  22288. dump tran master with no_log
  22289. go
  22290.  
  22291. if (charindex('8.00', @@version) > 0)
  22292.     drop procedure sp_tables_info_rowset
  22293. else
  22294. begin
  22295.     print ''
  22296.     print ''
  22297.     print 'Warning:'
  22298.     print 'you are installing the stored procedures '
  22299.     print 'on a pre 8.0 SQL Server.'
  22300.     print 'Ignore the following errors.'
  22301. end
  22302. go
  22303.  
  22304. /*  Procedure for 8.0 server */
  22305. create procedure sp_tables_info_rowset
  22306.     (
  22307.     @table_name sysname, 
  22308.     @table_schema   sysname = null, 
  22309.     @table_type nvarchar(255) = null 
  22310.     )
  22311. as
  22312.     select  *
  22313.     from    (select TABLE_CATALOG       = db_name(),
  22314.             TABLE_SCHEMA        = user_name(o.uid),
  22315.             TABLE_NAME      = o.name,
  22316.             TABLE_TYPE      = convert(nvarchar(30),
  22317.                             case o.type 
  22318.                             when 'U' then 
  22319.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22320.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22321.                             when 'S' then N'SYSTEM TABLE'
  22322.                             when 'V' then 
  22323.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22324.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22325.                             end),
  22326.             TABLE_GUID      = convert(uniqueidentifier, null),
  22327.             BOOKMARKS       = convert(bit, 1),
  22328.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22329.             BOOKMARK_DATATYPE   = convert(smallint, 19 /*DBTYPE_UI4 */),
  22330.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22331.             BOOKMARK_INFORMATION    = convert(int, 0),
  22332.             TABLE_VERSION       = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  22333.             CARDINALITY     = x.rows,
  22334.             DESCRIPTION     = convert(nvarchar(1), null),
  22335.             TABLE_PROPID        = convert(int, null) 
  22336.     
  22337.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22338.         where   o.name = @table_name
  22339.         and     o.type in ('U','V','S')
  22340.         and permissions(o.id) <> 0) as t
  22341.  
  22342.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22343.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22344.     order by 4, 2, 3
  22345. go
  22346. dump tran master with no_log
  22347. go
  22348. create procedure sp_tables_info_rowset;2
  22349.     (
  22350.     @table_schema   sysname = null, 
  22351.     @table_type nvarchar(255) = null 
  22352.     )
  22353. as
  22354.     select  *
  22355.     from    (select TABLE_CATALOG       = db_name(),
  22356.             TABLE_SCHEMA        = user_name(o.uid),
  22357.             TABLE_NAME      = o.name,
  22358.             TABLE_TYPE      = convert(nvarchar(30),
  22359.                             case o.type 
  22360.                             when 'U' then 
  22361.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22362.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22363.                             when 'S' then N'SYSTEM TABLE'
  22364.                             when 'V' then 
  22365.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22366.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22367.                             end),
  22368.             TABLE_GUID      = convert(uniqueidentifier, null),
  22369.             BOOKMARKS       = convert(bit, 1),
  22370.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22371.             BOOKMARK_DATATYPE   = convert(smallint, 19 /*DBTYPE_UI4 */),
  22372.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22373.             BOOKMARK_INFORMATION    = convert(int, 0),
  22374.             TABLE_VERSION       = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  22375.             CARDINALITY     = x.rows,
  22376.             DESCRIPTION     = convert(nvarchar(1), null),
  22377.             TABLE_PROPID        = convert(int, null) 
  22378.     
  22379.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22380.         where   o.type in ('U','V','S')
  22381.         and permissions(o.id) <> 0) as t
  22382.  
  22383.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22384.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22385.     order by 4, 2, 3
  22386. go
  22387.  
  22388. grant execute on sp_tables_info_rowset to public
  22389. go
  22390.  
  22391. dump tran master with no_log
  22392. go
  22393.  
  22394. if (charindex('6.00', @@version) > 0)
  22395.     begin
  22396.     if (exists (select * from sysobjects
  22397.         where name = 'sp_tables_info_rowset' and type = 'P '))
  22398.         begin
  22399.         drop procedure sp_tables_info_rowset
  22400.         dump tran master with no_log
  22401.         end
  22402.     end
  22403. go
  22404.  
  22405. print ''
  22406. print 'creating sp_tables_info_rowset_64'
  22407. go
  22408.  
  22409.  
  22410. /*  Procedure for 6.50 and earlier servers */
  22411. create procedure sp_tables_info_rowset_64
  22412.     (
  22413.     @table_name varchar(255), 
  22414.     @table_schema   varchar(255) = null,    
  22415.     @table_type varchar(255) = null 
  22416.     )
  22417. as
  22418.     select  TABLE_CATALOG       = db_name(),
  22419.         TABLE_SCHEMA        = user_name(o.uid),
  22420.         TABLE_NAME      = o.name,
  22421.         TABLE_TYPE      = convert(varchar(30),
  22422.                         case o.type 
  22423.                         when 'U' then 'TABLE'
  22424.                         when 'V' then 'VIEW'
  22425.                         when 'S' then 'SYSTEM TABLE'
  22426.                         end),
  22427.         TABLE_GUID      = convert(binary(16), null),
  22428.         BOOKMARKS       = convert(bit, 1),
  22429.         BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22430.         BOOKMARK_DATATYPE   = convert(smallint, 21 /*DBTYPE_UI8 */),
  22431.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22432.         BOOKMARK_INFORMATION    = convert(int, 0),
  22433.         TABLE_VERSION       = convert(int, o.schema_ver),
  22434.         CARDINALITY     = x.rows,
  22435.         DESCRIPTION     = convert(varchar(1), null),
  22436.         TABLE_PROPID        = convert(int, null)
  22437.  
  22438.     from    sysusers u, 
  22439.         sysobjects o,
  22440.         sysindexes x
  22441.  
  22442.     where   o.type in ('U','V','S')
  22443.     and     o.name = @table_name
  22444.     and     (   @table_schema is null
  22445.         or  @table_schema = user_name(o.uid)
  22446.         )
  22447.     and     (
  22448.             @table_type is null
  22449.         or  @table_type = case o.type 
  22450.                     when 'U' then 'TABLE'
  22451.                     when 'V' then 'VIEW'
  22452.                     when 'S' then 'SYSTEM TABLE'
  22453.                     end
  22454.         )
  22455.     and o.id *= x.id
  22456.     and x.indid in (0,1)
  22457.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  22458.     and     (
  22459.         suser_id() = 1   /* User is the System Administrator */
  22460.         or o.uid = user_id()     /* User created the object */
  22461.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  22462.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  22463.             from sysprotects p
  22464.             /* join to correlate with all rows in sysobjects */
  22465.             where p.id = o.id
  22466.             /* get rows for public,current user,user's group */
  22467.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  22468.             /* check for SELECT,EXECUTE privilege */
  22469.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  22470.             ) = 1   /* final magic...compare Grants   */
  22471.         )       
  22472.     order by 4, 2, 3
  22473. go
  22474. dump tran master with no_log
  22475. go
  22476. create procedure sp_tables_info_rowset_64;2
  22477.     (
  22478.     @table_schema   varchar(255) = null,    
  22479.     @table_type varchar(255) = null 
  22480.     )
  22481. as
  22482.     select  TABLE_CATALOG       = db_name(),
  22483.         TABLE_SCHEMA        = user_name(o.uid),
  22484.         TABLE_NAME      = o.name,
  22485.         TABLE_TYPE      = convert(varchar(30),
  22486.                         case o.type 
  22487.                         when 'U' then 'TABLE'
  22488.                         when 'V' then 'VIEW'
  22489.                         when 'S' then 'SYSTEM TABLE'
  22490.                         end),
  22491.         TABLE_GUID      = convert(binary(16), null),
  22492.         BOOKMARKS       = convert(bit, 1),
  22493.         BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22494.         BOOKMARK_DATATYPE   = convert(smallint, 21 /*DBTYPE_UI8 */),
  22495.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22496.         BOOKMARK_INFORMATION    = convert(int, 0),
  22497.         TABLE_VERSION       = convert(int, o.schema_ver),
  22498.         CARDINALITY     = x.rows,
  22499.         DESCRIPTION     = convert(varchar(1), null),
  22500.         TABLE_PROPID        = convert(int, null)
  22501.  
  22502.     from    sysusers u, 
  22503.         sysobjects o,
  22504.         sysindexes x
  22505.  
  22506.     where   o.type in ('U','V','S')
  22507.     and     (   @table_schema is null
  22508.         or  @table_schema = user_name(o.uid)
  22509.         )
  22510.     and     (
  22511.             @table_type is null
  22512.         or  @table_type = case o.type 
  22513.                     when 'U' then 'TABLE'
  22514.                     when 'V' then 'VIEW'
  22515.                     when 'S' then 'SYSTEM TABLE'
  22516.                     end
  22517.         )
  22518.     and o.id *= x.id
  22519.     and x.indid in (0,1)
  22520.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  22521.     and     (
  22522.         suser_id() = 1   /* User is the System Administrator */
  22523.         or o.uid = user_id()     /* User created the object */
  22524.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  22525.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  22526.             from sysprotects p
  22527.             /* join to correlate with all rows in sysobjects */
  22528.             where p.id = o.id
  22529.             /* get rows for public,current user,user's group */
  22530.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  22531.             /* check for SELECT,EXECUTE privilege */
  22532.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  22533.             ) = 1   /* final magic...compare Grants   */
  22534.         )       
  22535.     order by 4, 2, 3
  22536. go
  22537. dump tran master with no_log
  22538. go
  22539.  
  22540. if (charindex('7.00', @@version) > 0 or
  22541.     charindex('8.00', @@version) > 0)
  22542.     drop procedure sp_tables_info_rowset_64
  22543. else
  22544. begin
  22545.     print ''
  22546.     print ''
  22547.     print 'Warning:'
  22548.     print 'you are installing the stored procedures '
  22549.     print 'on a pre 7.0 SQL Server.'
  22550.     print 'Ignore the following errors.'
  22551. end
  22552. go
  22553.  
  22554. /*  Procedure for 7.0 server */
  22555. create procedure sp_tables_info_rowset_64
  22556.     (
  22557.     @table_name sysname, 
  22558.     @table_schema   sysname = null, 
  22559.     @table_type nvarchar(255) = null 
  22560.     )
  22561. as
  22562.     select  *
  22563.     from    (select TABLE_CATALOG       = db_name(),
  22564.             TABLE_SCHEMA        = user_name(o.uid),
  22565.             TABLE_NAME      = o.name,
  22566.             TABLE_TYPE      = convert(nvarchar(30),
  22567.                             case o.type 
  22568.                             when 'U' then 
  22569.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22570.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22571.                             when 'S' then N'SYSTEM TABLE'
  22572.                             when 'V' then 
  22573.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22574.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22575.                             end),
  22576.             TABLE_GUID      = convert(uniqueidentifier, null),
  22577.             BOOKMARKS       = convert(bit, 1),
  22578.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22579.             BOOKMARK_DATATYPE   = convert(smallint, 21 /*DBTYPE_UI8 */),
  22580.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22581.             BOOKMARK_INFORMATION    = convert(int, 0),
  22582.             TABLE_VERSION       = convert(int, o.schema_ver),
  22583.             CARDINALITY     = x.rows,
  22584.             DESCRIPTION     = convert(nvarchar(1), null),
  22585.             TABLE_PROPID        = convert(int, null) 
  22586.     
  22587.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22588.         where   o.name = @table_name
  22589.         and     o.type in ('U','V','S')
  22590.         and permissions(o.id) <> 0) as t
  22591.  
  22592.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22593.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22594.     order by 4, 2, 3
  22595. go
  22596. dump tran master with no_log
  22597. go
  22598. create procedure sp_tables_info_rowset_64;2
  22599.     (
  22600.     @table_schema   sysname = null, 
  22601.     @table_type nvarchar(255) = null 
  22602.     )
  22603. as
  22604.     select  *
  22605.     from    (select TABLE_CATALOG       = db_name(),
  22606.             TABLE_SCHEMA        = user_name(o.uid),
  22607.             TABLE_NAME      = o.name,
  22608.             TABLE_TYPE      = convert(nvarchar(30),
  22609.                             case o.type 
  22610.                             when 'U' then 
  22611.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22612.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22613.                             when 'S' then N'SYSTEM TABLE'
  22614.                             when 'V' then 
  22615.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22616.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22617.                             end),
  22618.             TABLE_GUID      = convert(uniqueidentifier, null),
  22619.             BOOKMARKS       = convert(bit, 1),
  22620.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22621.             BOOKMARK_DATATYPE   = convert(smallint, 21 /*DBTYPE_UI8 */),
  22622.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22623.             BOOKMARK_INFORMATION    = convert(int, 0),
  22624.             TABLE_VERSION       = convert(int, o.schema_ver),
  22625.             CARDINALITY     = x.rows,
  22626.             DESCRIPTION     = convert(nvarchar(1), null),
  22627.             TABLE_PROPID        = convert(int, null) 
  22628.     
  22629.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22630.         where   o.type in ('U','V','S')
  22631.         and permissions(o.id) <> 0) as t
  22632.  
  22633.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22634.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22635.     order by 4, 2, 3
  22636. go
  22637.  
  22638. dump tran master with no_log
  22639. go
  22640.  
  22641. if (charindex('8.00', @@version) > 0)
  22642.     drop procedure sp_tables_info_rowset_64
  22643. else
  22644. begin
  22645.     print ''
  22646.     print ''
  22647.     print 'Warning:'
  22648.     print 'you are installing the stored procedures '
  22649.     print 'on a pre 8.0 SQL Server.'
  22650.     print 'Ignore the following errors.'
  22651. end
  22652. go
  22653.  
  22654. /*  Procedure for 8.0 server */
  22655. create procedure sp_tables_info_rowset_64
  22656.     (
  22657.     @table_name sysname, 
  22658.     @table_schema   sysname = null, 
  22659.     @table_type nvarchar(255) = null 
  22660.     )
  22661. as
  22662.     select  *
  22663.     from    (select TABLE_CATALOG       = db_name(),
  22664.             TABLE_SCHEMA        = user_name(o.uid),
  22665.             TABLE_NAME      = o.name,
  22666.             TABLE_TYPE      = convert(nvarchar(30),
  22667.                             case o.type 
  22668.                             when 'U' then 
  22669.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22670.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22671.                             when 'S' then N'SYSTEM TABLE'
  22672.                             when 'V' then 
  22673.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22674.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22675.                             end),
  22676.             TABLE_GUID      = convert(uniqueidentifier, null),
  22677.             BOOKMARKS       = convert(bit, 1),
  22678.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22679.             BOOKMARK_DATATYPE   = convert(smallint, 21 /*DBTYPE_UI8 */),
  22680.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22681.             BOOKMARK_INFORMATION    = convert(int, 0),
  22682.             TABLE_VERSION       = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  22683.             CARDINALITY     = x.rows,
  22684.             DESCRIPTION     = convert(nvarchar(1), null),
  22685.             TABLE_PROPID        = convert(int, null) 
  22686.     
  22687.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22688.         where   o.name = @table_name
  22689.         and     o.type in ('U','V','S')
  22690.         and permissions(o.id) <> 0) as t
  22691.  
  22692.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22693.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22694.     order by 4, 2, 3
  22695. go
  22696. dump tran master with no_log
  22697. go
  22698. create procedure sp_tables_info_rowset_64;2
  22699.     (
  22700.     @table_schema   sysname = null, 
  22701.     @table_type nvarchar(255) = null 
  22702.     )
  22703. as
  22704.     select  *
  22705.     from    (select TABLE_CATALOG       = db_name(),
  22706.             TABLE_SCHEMA        = user_name(o.uid),
  22707.             TABLE_NAME      = o.name,
  22708.             TABLE_TYPE      = convert(nvarchar(30),
  22709.                             case o.type 
  22710.                             when 'U' then 
  22711.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22712.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22713.                             when 'S' then N'SYSTEM TABLE'
  22714.                             when 'V' then 
  22715.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22716.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22717.                             end),
  22718.             TABLE_GUID      = convert(uniqueidentifier, null),
  22719.             BOOKMARKS       = convert(bit, 1),
  22720.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22721.             BOOKMARK_DATATYPE   = convert(smallint, 21 /*DBTYPE_UI8 */),
  22722.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22723.             BOOKMARK_INFORMATION    = convert(int, 0),
  22724.             TABLE_VERSION       = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  22725.             CARDINALITY     = x.rows,
  22726.             DESCRIPTION     = convert(nvarchar(1), null),
  22727.             TABLE_PROPID        = convert(int, null) 
  22728.     
  22729.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22730.         where   o.type in ('U','V','S')
  22731.         and permissions(o.id) <> 0) as t
  22732.  
  22733.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22734.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22735.     order by 4, 2, 3
  22736. go
  22737.  
  22738. grant execute on sp_tables_info_rowset_64 to public
  22739. go
  22740.  
  22741. dump tran master with no_log
  22742. go
  22743.  
  22744. if (charindex('6.00', @@version) > 0)
  22745.     begin
  22746.     if (exists (select * from sysobjects
  22747.         where name = 'sp_tables_info_rowset_64' and type = 'P '))
  22748.         begin
  22749.         drop procedure sp_tables_info_rowset_64
  22750.         dump tran master with no_log
  22751.         end
  22752.     end
  22753. go
  22754.  
  22755. print ''
  22756. print 'creating sp_table_constraints_rowset'
  22757. go
  22758.  
  22759.  
  22760. /*  Procedure for 6.50 and earlier servers */
  22761. create procedure sp_table_constraints_rowset
  22762.     (
  22763.     @table_name         varchar(255), 
  22764.     @table_schema       varchar(255) = null,
  22765.     @table_catalog      varchar(255) = null,
  22766.     @constraint_name    varchar(255) = null,
  22767.     @constraint_schema  varchar(255) = null,
  22768.     @constraint_catalog varchar(255) = null,
  22769.     @constraint_type    varchar(255) = null 
  22770.     )
  22771. as
  22772.     select
  22773.         CONSTRAINT_CATALOG  = db_name(),
  22774.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  22775.         CONSTRAINT_NAME     = c_obj.name,
  22776.         TABLE_CATALOG       = db_name(),
  22777.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22778.         TABLE_NAME          = t_obj.name,
  22779.         CONSTRAINT_TYPE     = case (c.status & 0xf)
  22780.                                 when 1 then 'PRIMARY KEY'
  22781.                                 when 2 then 'UNIQUE'
  22782.                                 when 3 then 'FOREIGN KEY'
  22783.                                 when 4 then 'CHECK'
  22784.                                 end, 
  22785.         IS_DEFERRABLE       = convert(tinyint, 0),
  22786.         INITIALLY_DEFERRED  = convert(tinyint, 0),
  22787.         DESCRIPTION         = convert(varchar(1), null)
  22788.  
  22789.     from 
  22790.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22791.     where
  22792.             t_obj.name  = @table_name
  22793.         and t_obj.type in ('U','S')
  22794.         and (@table_catalog is null or @table_catalog = db_name())
  22795.         and (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22796.         and c.id = t_obj.id
  22797.         and (c.status & 0xf) between 1 and 4
  22798.         and c_obj.id    = c.constid
  22799.         and c_obj.uid   = user_id()
  22800.         and (@constraint_name is null or c_obj.name = @constraint_name)
  22801.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22802.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22803.         and (@constraint_type is null
  22804.             or (c.status & 0xf)
  22805.                 = case @constraint_type
  22806.                     when 'PRIMARY KEY' then 1
  22807.                     when 'UNIQUE' then 2
  22808.                     when 'FOREIGN KEY' then 3
  22809.                     when 'CHECK' then 4
  22810.                     end)
  22811. order by 2,3,5,6,7 
  22812. go
  22813. dump tran master with no_log
  22814. go
  22815. create procedure sp_table_constraints_rowset;2
  22816.     (
  22817.     @table_schema       varchar(255) = null,
  22818.     @table_catalog      varchar(255) = null,
  22819.     @constraint_name    varchar(255) = null,
  22820.     @constraint_schema  varchar(255) = null,
  22821.     @constraint_catalog varchar(255) = null,
  22822.     @constraint_type    varchar(255) = null 
  22823.     )
  22824. as
  22825.     select
  22826.         CONSTRAINT_CATALOG  = db_name(),
  22827.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  22828.         CONSTRAINT_NAME     = c_obj.name,
  22829.         TABLE_CATALOG       = db_name(),
  22830.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22831.         TABLE_NAME          = t_obj.name,
  22832.         CONSTRAINT_TYPE     = case (c.status & 0xf)
  22833.                                 when 1 then 'PRIMARY KEY'
  22834.                                 when 2 then 'UNIQUE'
  22835.                                 when 3 then 'FOREIGN KEY'
  22836.                                 when 4 then 'CHECK'
  22837.                                 end, 
  22838.         IS_DEFERRABLE       = convert(tinyint, 0),
  22839.         INITIALLY_DEFERRED  = convert(tinyint, 0),
  22840.         DESCRIPTION         = convert(varchar(1), null)
  22841.  
  22842.     from 
  22843.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22844.     where
  22845.             t_obj.type in ('U','S')
  22846.         and (@table_catalog is null or @table_catalog = db_name())
  22847.         and (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22848.         and c.id = t_obj.id
  22849.         and (c.status & 0xf) between 1 and 4
  22850.         and c_obj.id    = c.constid
  22851.         and c_obj.uid   = user_id()
  22852.         and (@constraint_name is null or c_obj.name = @constraint_name)
  22853.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22854.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22855.         and (@constraint_type is null
  22856.             or (c.status & 0xf)
  22857.                 = case @constraint_type
  22858.                     when 'PRIMARY KEY' then 1
  22859.                     when 'UNIQUE' then 2
  22860.                     when 'FOREIGN KEY' then 3
  22861.                     when 'CHECK' then 4
  22862.                     end)
  22863. order by 2,3,5,6,7 
  22864. go
  22865. dump tran master with no_log
  22866. go
  22867.  
  22868.  
  22869. if (charindex('7.00', @@version) > 0 or
  22870.     charindex('8.00', @@version) > 0)
  22871.     drop procedure sp_table_constraints_rowset
  22872. else
  22873. begin
  22874.     print ''
  22875.     print ''
  22876.     print 'Warning:'
  22877.     print 'you are installing the stored procedures '
  22878.     print 'on a pre 8.0 SQL Server.'
  22879.     print 'Ignore the following errors.'
  22880. end
  22881. go
  22882.  
  22883. /*  Procedure for 8.0 server */
  22884. create procedure sp_table_constraints_rowset
  22885.     (
  22886.     @table_name         sysname, 
  22887.     @table_schema       sysname = null,
  22888.     @table_catalog      sysname = null,
  22889.     @constraint_name    sysname = null,
  22890.     @constraint_schema  sysname = null,
  22891.     @constraint_catalog sysname = null,
  22892.     @constraint_type    nvarchar(255) = null 
  22893.     )
  22894. as
  22895.     select
  22896.         CONSTRAINT_CATALOG  = db_name(),
  22897.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  22898.         CONSTRAINT_NAME     = c_obj.name,
  22899.         TABLE_CATALOG       = db_name(),
  22900.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22901.         TABLE_NAME          = t_obj.name,
  22902.         CONSTRAINT_TYPE     = case (c.status & 0xf)
  22903.                                 when 1 then N'PRIMARY KEY'
  22904.                                 when 2 then N'UNIQUE'
  22905.                                 when 3 then N'FOREIGN KEY'
  22906.                                 when 4 then N'CHECK'
  22907.                                 end, 
  22908.         IS_DEFERRABLE       = convert(bit, 0),
  22909.         INITIALLY_DEFERRED  = convert(bit, 0),
  22910.         DESCRIPTION         = convert(nvarchar(1), null)
  22911.  
  22912.     from 
  22913.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22914.     where
  22915.             t_obj.name  = @table_name
  22916.         and t_obj.type in ('U','S')
  22917.         and (@table_catalog is null or @table_catalog = db_name())
  22918.         and (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22919.         and c.id = t_obj.id
  22920.         and (c.status & 0xf) between 1 and 4
  22921.         and c_obj.id    = c.constid
  22922.         and c_obj.uid   = user_id()
  22923.         and (@constraint_name is null or c_obj.name = @constraint_name)
  22924.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22925.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22926.         and (@constraint_type is null
  22927.             or (c.status & 0xf)
  22928.                 = case @constraint_type
  22929.                     when N'PRIMARY KEY' then 1
  22930.                     when N'UNIQUE' then 2
  22931.                     when N'FOREIGN KEY' then 3
  22932.                     when N'CHECK' then 4
  22933.                     end)
  22934. order by 2,3,5,6,7 
  22935. go
  22936. dump tran master with no_log
  22937. go
  22938. create procedure sp_table_constraints_rowset;2
  22939.     (
  22940.     @table_schema       sysname = null,
  22941.     @table_catalog      sysname = null,
  22942.     @constraint_name    sysname = null,
  22943.     @constraint_schema  sysname = null,
  22944.     @constraint_catalog sysname = null,
  22945.     @constraint_type    nvarchar(255) = null 
  22946.     )
  22947. as
  22948.     select
  22949.         CONSTRAINT_CATALOG  = db_name(),
  22950.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  22951.         CONSTRAINT_NAME     = c_obj.name,
  22952.         TABLE_CATALOG       = db_name(),
  22953.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22954.         TABLE_NAME          = t_obj.name,
  22955.         CONSTRAINT_TYPE     = case (c.status & 0xf)
  22956.                                 when 1 then N'PRIMARY KEY'
  22957.                                 when 2 then N'UNIQUE'
  22958.                                 when 3 then N'FOREIGN KEY'
  22959.                                 when 4 then N'CHECK'
  22960.                                 end, 
  22961.         IS_DEFERRABLE       = convert(bit, 0),
  22962.         INITIALLY_DEFERRED  = convert(bit, 0),
  22963.         DESCRIPTION         = convert(nvarchar(1), null)
  22964.  
  22965.     from 
  22966.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22967.     where
  22968.             t_obj.type in ('U','S')
  22969.         and (@table_catalog is null or @table_catalog = db_name())
  22970.         and (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22971.         and c.id = t_obj.id
  22972.         and (c.status & 0xf) between 1 and 4
  22973.         and c_obj.id    = c.constid
  22974.         and c_obj.uid   = user_id()
  22975.         and (@constraint_name is null or c_obj.name = @constraint_name)
  22976.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22977.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22978.         and (@constraint_type is null
  22979.             or (c.status & 0xf)
  22980.                 = case @constraint_type
  22981.                     when N'PRIMARY KEY' then 1
  22982.                     when N'UNIQUE' then 2
  22983.                     when N'FOREIGN KEY' then 3
  22984.                     when N'CHECK' then 4
  22985.                     end)
  22986. order by 2,3,5,6,7 
  22987. go
  22988. dump tran master with no_log
  22989.  
  22990.  
  22991. grant execute on sp_table_constraints_rowset to public
  22992. go
  22993.  
  22994. dump tran master with no_log
  22995. go
  22996. if (charindex('6.00', @@version) > 0)
  22997.     begin
  22998.     if (exists (select * from sysobjects
  22999.             where name = 'sp_table_constraints_rowset' and type = 'P '))
  23000.         begin
  23001.         drop procedure sp_table_constraints_rowset
  23002.         dump tran master with no_log
  23003.         end
  23004.     end
  23005. go
  23006.  
  23007. print ''
  23008. print 'creating sp_table_privileges_rowset'
  23009. go
  23010.  
  23011. /*  Procedure for 6.0 and 6.5 server */
  23012. CREATE PROCEDURE sp_table_privileges_rowset
  23013.     (
  23014.     @table_name varchar(255) = null,
  23015.     @table_schema   varchar(255) = null,
  23016.     @grantor    varchar(255) = null,
  23017.     @grantee    varchar(255) = null
  23018.     )
  23019. as
  23020. IF @table_name is not null
  23021.     BEGIN
  23022.     select
  23023.         GRANTOR     = user_name(p.grantor),
  23024.         GRANTEE     = user_name(u.uid),
  23025.         TABLE_CATALOG   = db_name(),
  23026.         TABLE_SCHEMA    = user_name(o.uid),
  23027.         TABLE_NAME  = o.name,
  23028.         PRIVILEGE_TYPE  = convert(varchar(30),
  23029.                     case p.action
  23030.                     when 193 then 'SELECT'
  23031.                     when 195 then 'INSERT'
  23032.                     when 196 then 'DELETE'
  23033.                     when 197 then 'UPDATE'
  23034.                     else 'REFERENCES'
  23035.                     end),
  23036.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  23037.     from 
  23038.         sysprotects p, sysobjects o, sysusers u
  23039.     where
  23040.         o.name = @table_name
  23041.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23042.     and (@grantee is null or @grantee = user_name(u.uid))
  23043.     and     o.type in ('U','V','S')
  23044.     and p.id = o.id
  23045.     and     (@grantor is null or @grantor = user_name(p.grantor))
  23046.             /* expand groups */
  23047.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  23048.     and     p.protecttype <> 206    /* only grant rows */
  23049.     and     p.action in (26,193,195,196,197)
  23050.     and     o.uid <> u.uid          /* no rows for owner */
  23051.     and     not exists (            /* exclude revoke'd privileges */
  23052.             select  *
  23053.             from    sysprotects p1
  23054.             where   p1.protecttype = 206
  23055.             and     p1.action = p.action
  23056.             and     p1.id = p.id
  23057.             and     p1.uid = u.uid)
  23058.     union
  23059.     select  /*  Add rows for table owner */
  23060.         GRANTOR     = user_name(u.uid),
  23061.         GRANTEE     = user_name(o.uid),
  23062.         TABLE_CATALOG   = db_name(),
  23063.         TABLE_SCHEMA    = user_name(o.uid),
  23064.         TABLE_NAME  = o.name,
  23065.         PRIVILEGE_TYPE  = convert(varchar(30),
  23066.                     case v.number
  23067.                     when 193 then 'SELECT'
  23068.                     when 195 then 'INSERT'
  23069.                     when 196 then 'DELETE'
  23070.                     when 197 then 'UPDATE'
  23071.                     else 'REFERENCES'
  23072.                     end),
  23073.         IS_GRANTABLE    = convert(bit,1)    
  23074.     from 
  23075.         sysobjects o, master.dbo.spt_values v, sysusers u
  23076.     where
  23077.         o.name = @table_name
  23078.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23079.     and (@grantee is null or @grantee = user_name(o.uid))
  23080.     and     o.type in ('U','V','S')
  23081.     and     u.uid = 1       /* grantor is dbo of database */
  23082.     and (@grantor is null or @grantor = user_name(u.uid))
  23083.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23084.     and     v.number in (26,193,195,196,197)
  23085.     and     not exists (    /* exclude revoke'd privileges */
  23086.             select  *
  23087.             from    sysprotects p1
  23088.             where   p1.protecttype = 206
  23089.             and     p1.action = v.number
  23090.             and     p1.id = o.id
  23091.             and     p1.uid = o.uid)
  23092.     order by 4,5,6,1,2
  23093.     END
  23094. ELSE
  23095.     BEGIN
  23096.     select
  23097.         GRANTOR     = user_name(p.grantor),
  23098.         GRANTEE     = user_name(u.uid),
  23099.         TABLE_CATALOG   = db_name(),
  23100.         TABLE_SCHEMA    = user_name(o.uid),
  23101.         TABLE_NAME  = o.name,
  23102.         PRIVILEGE_TYPE  = convert(varchar(30),
  23103.                     case p.action
  23104.                     when 193 then 'SELECT'
  23105.                     when 195 then 'INSERT'
  23106.                     when 196 then 'DELETE'
  23107.                     when 197 then 'UPDATE'
  23108.                     else 'REFERENCES'
  23109.                     end),
  23110.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  23111.     from 
  23112.         sysprotects p, sysobjects o, sysusers u
  23113.     where
  23114.         o.type in ('U','V','S')
  23115.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23116.     and (@grantee is null or @grantee = user_name(u.uid))
  23117.     and p.id = o.id
  23118.     and     (@grantor is null or @grantor = user_name(p.grantor))
  23119.             /* expand groups */
  23120.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  23121.     and     p.protecttype <> 206    /* only grant rows */
  23122.     and     p.action in (26,193,195,196,197)
  23123.     and     o.uid <> u.uid          /* no rows for owner */
  23124.     and     not exists (            /* exclude revoke'd privileges */
  23125.             select  *
  23126.             from    sysprotects p1
  23127.             where   p1.protecttype = 206
  23128.             and     p1.action = p.action
  23129.             and     p1.id = p.id
  23130.             and     p1.uid = u.uid)
  23131.     union
  23132.     select  /*  Add rows for table owner */
  23133.         GRANTOR     = user_name(u.uid),
  23134.         GRANTEE     = user_name(o.uid),
  23135.         TABLE_CATALOG   = db_name(),
  23136.         TABLE_SCHEMA    = user_name(o.uid),
  23137.         TABLE_NAME  = o.name,
  23138.         PRIVILEGE_TYPE  = convert(varchar(30),
  23139.                     case v.number
  23140.                     when 193 then 'SELECT'
  23141.                     when 195 then 'INSERT'
  23142.                     when 196 then 'DELETE'
  23143.                     when 197 then 'UPDATE'
  23144.                     else 'REFERENCES'
  23145.                     end),
  23146.         IS_GRANTABLE    = convert(bit,1)    
  23147.     from 
  23148.         sysobjects o, master.dbo.spt_values v, sysusers u
  23149.     where
  23150.         o.type in ('U','V','S')
  23151.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23152.     and (@grantee is null or @grantee = user_name(o.uid))
  23153.     and     u.uid = 1       /* grantor is dbo of database */
  23154.     and (@grantor is null or @grantor = user_name(u.uid))
  23155.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23156.     and     v.number in (26,193,195,196,197)
  23157.     and     not exists (    /* exclude revoke'd privileges */
  23158.             select  *
  23159.             from    sysprotects p1
  23160.             where   p1.protecttype = 206
  23161.             and     p1.action = v.number
  23162.             and     p1.id = o.id
  23163.             and     p1.uid = o.uid)
  23164.     order by 4,5,6,1,2
  23165.     END
  23166. go
  23167. dump tran master with no_log
  23168. go
  23169. CREATE PROCEDURE sp_table_privileges_rowset;2
  23170.     (
  23171.     @handle     int output,
  23172.     @scrollopt  int output,
  23173.     @ccopt      int output,
  23174.     @rows       int output,
  23175.     @table_name     varchar(255) = null,
  23176.     @table_schema   varchar(255) = null,
  23177.     @grantor    varchar(255) = null,
  23178.     @grantee    varchar(255) = null
  23179.     )
  23180. as
  23181. declare @ret int
  23182. SET NOCOUNT ON
  23183. create table #sptprivsrowset1
  23184.     (
  23185.     GRANTOR     sysname not null,
  23186.     GRANTEE     sysname not null,
  23187.     TABLE_CATALOG   sysname not null,
  23188.     TABLE_SCHEMA    sysname not null,
  23189.     TABLE_NAME  sysname not null,
  23190.     PRIVILEGE_TYPE  sysname not null,
  23191.     IS_GRANTABLE    bit not null
  23192.     )
  23193.     
  23194. IF @table_name is not null
  23195.     BEGIN
  23196.     insert into #sptprivsrowset1
  23197.     select
  23198.         GRANTOR     = user_name(p.grantor),
  23199.         GRANTEE     = user_name(u.uid),
  23200.         TABLE_CATALOG   = db_name(),
  23201.         TABLE_SCHEMA    = user_name(o.uid),
  23202.         TABLE_NAME  = o.name,
  23203.         PRIVILEGE_TYPE  = convert(varchar(30),
  23204.                     case p.action
  23205.                     when 193 then 'SELECT'
  23206.                     when 195 then 'INSERT'
  23207.                     when 196 then 'DELETE'
  23208.                     when 197 then 'UPDATE'
  23209.                     else 'REFERENCES'
  23210.                     end),
  23211.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  23212.     from 
  23213.         sysprotects p, sysobjects o, sysusers u
  23214.     where
  23215.         o.name = @table_name
  23216.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23217.     and (@grantee is null or @grantee = user_name(u.uid))
  23218.     and     o.type in ('U','V','S')
  23219.     and p.id = o.id
  23220.     and     (@grantor is null or @grantor = user_name(p.grantor))
  23221.             /* expand groups */
  23222.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  23223.     and     p.protecttype <> 206    /* only grant rows */
  23224.     and     p.action in (26,193,195,196,197)
  23225.     and     o.uid <> u.uid          /* no rows for owner */
  23226.     and     not exists (            /* exclude revoke'd privileges */
  23227.             select  *
  23228.             from    sysprotects p1
  23229.             where   p1.protecttype = 206
  23230.             and     p1.action = p.action
  23231.             and     p1.id = p.id
  23232.             and     p1.uid = u.uid)
  23233.     union
  23234.     select  /*  Add rows for table owner */
  23235.         GRANTOR     = user_name(u.uid),
  23236.         GRANTEE     = user_name(o.uid),
  23237.         TABLE_CATALOG   = db_name(),
  23238.         TABLE_SCHEMA    = user_name(o.uid),
  23239.         TABLE_NAME  = o.name,
  23240.         PRIVILEGE_TYPE  = convert(varchar(30),
  23241.                     case v.number
  23242.                     when 193 then 'SELECT'
  23243.                     when 195 then 'INSERT'
  23244.                     when 196 then 'DELETE'
  23245.                     when 197 then 'UPDATE'
  23246.                     else 'REFERENCES'
  23247.                     end),
  23248.         IS_GRANTABLE    = convert(bit,1)    
  23249.     from 
  23250.         sysobjects o, master.dbo.spt_values v, sysusers u
  23251.     where
  23252.         o.name = @table_name
  23253.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23254.     and (@grantee is null or @grantee = user_name(o.uid))
  23255.     and     o.type in ('U','V','S')
  23256.     and     u.uid = 1       /* grantor is dbo of database */
  23257.     and (@grantor is null or @grantor = user_name(u.uid))
  23258.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23259.     and     v.number in (26,193,195,196,197)
  23260.     and     not exists (    /* exclude revoke'd privileges */
  23261.             select  *
  23262.             from    sysprotects p1
  23263.             where   p1.protecttype = 206
  23264.             and     p1.action = v.number
  23265.             and     p1.id = o.id
  23266.             and     p1.uid = o.uid)
  23267.     order by 4,5,6,1,2
  23268.     END
  23269. ELSE
  23270.     BEGIN
  23271.     insert into #sptprivsrowset1
  23272.     select
  23273.         GRANTOR     = user_name(p.grantor),
  23274.         GRANTEE     = user_name(u.uid),
  23275.         TABLE_CATALOG   = db_name(),
  23276.         TABLE_SCHEMA    = user_name(o.uid),
  23277.         TABLE_NAME  = o.name,
  23278.         PRIVILEGE_TYPE  = convert(varchar(30),
  23279.                     case p.action
  23280.                     when 193 then 'SELECT'
  23281.                     when 195 then 'INSERT'
  23282.                     when 196 then 'DELETE'
  23283.                     when 197 then 'UPDATE'
  23284.                     else 'REFERENCES'
  23285.                     end),
  23286.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  23287.     from 
  23288.         sysprotects p, sysobjects o, sysusers u
  23289.     where
  23290.         o.type in ('U','V','S')
  23291.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23292.     and (@grantee is null or @grantee = user_name(u.uid))
  23293.     and p.id = o.id
  23294.     and     (@grantor is null or @grantor = user_name(p.grantor))
  23295.             /* expand groups */
  23296.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  23297.     and     p.protecttype <> 206    /* only grant rows */
  23298.     and     p.action in (26,193,195,196,197)
  23299.     and     o.uid <> u.uid          /* no rows for owner */
  23300.     and     not exists (            /* exclude revoke'd privileges */
  23301.             select  *
  23302.             from    sysprotects p1
  23303.             where   p1.protecttype = 206
  23304.             and     p1.action = p.action
  23305.             and     p1.id = p.id
  23306.             and     p1.uid = u.uid)
  23307.     union
  23308.     select  /*  Add rows for table owner */
  23309.         GRANTOR     = user_name(u.uid),
  23310.         GRANTEE     = user_name(o.uid),
  23311.         TABLE_CATALOG   = db_name(),
  23312.         TABLE_SCHEMA    = user_name(o.uid),
  23313.         TABLE_NAME  = o.name,
  23314.         PRIVILEGE_TYPE  = convert(varchar(30),
  23315.                     case v.number
  23316.                     when 193 then 'SELECT'
  23317.                     when 195 then 'INSERT'
  23318.                     when 196 then 'DELETE'
  23319.                     when 197 then 'UPDATE'
  23320.                     else 'REFERENCES'
  23321.                     end),
  23322.         IS_GRANTABLE    = convert(bit,1)    
  23323.     from 
  23324.         sysobjects o, master.dbo.spt_values v, sysusers u
  23325.     where
  23326.         o.type in ('U','V','S')
  23327.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23328.     and (@grantee is null or @grantee = user_name(o.uid))
  23329.     and     u.uid = 1       /* grantor is dbo of database */
  23330.     and (@grantor is null or @grantor = user_name(u.uid))
  23331.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23332.     and     v.number in (26,193,195,196,197)
  23333.     and     not exists (    /* exclude revoke'd privileges */
  23334.             select  *
  23335.             from    sysprotects p1
  23336.             where   p1.protecttype = 206
  23337.             and     p1.action = v.number
  23338.             and     p1.id = o.id
  23339.             and     p1.uid = o.uid)
  23340.     order by 4,5,6,1,2
  23341.     END
  23342.  
  23343. exec @ret = sp_cursoropen @handle output, 'select * from #sptprivsrowset1',
  23344.     @scrollopt output, @ccopt output, @rows output
  23345.  
  23346. drop table #sptprivsrowset1
  23347. return isnull(@ret,0)
  23348. go
  23349. dump tran master with no_log
  23350. go
  23351. CREATE PROCEDURE sp_table_privileges_rowset;3
  23352. as
  23353.     select
  23354.         GRANTOR     = convert(sysname, ' '),
  23355.         GRANTEE     = convert(sysname, ' '),
  23356.         TABLE_CATALOG   = convert(sysname, ' '),
  23357.         TABLE_SCHEMA    = convert(sysname, ' '),
  23358.         TABLE_NAME  = convert(sysname, ' '),
  23359.         PRIVILEGE_TYPE  = convert(varchar(30), ' '),
  23360.         IS_GRANTABLE    = convert(bit, 0)
  23361.     where   1=0
  23362. go
  23363.  
  23364. if (charindex('7.00', @@version) = 0 and
  23365.     charindex('8.00', @@version) = 0)
  23366. begin
  23367.     print ''
  23368.     print ''
  23369.     print 'Warning:'
  23370.     print 'you are installing the stored procedures '
  23371.     print 'on a pre 8.0 SQL Server.'
  23372.     print 'Ignore the following errors.'
  23373. end
  23374. else
  23375.     drop proc sp_table_privileges_rowset
  23376. go
  23377.  
  23378.  
  23379. /*  Procedure for 8.0 server */
  23380. CREATE PROCEDURE sp_table_privileges_rowset
  23381.     (
  23382.     @table_name         sysname,
  23383.     @table_schema       sysname = null,
  23384.     @grantor        sysname = null,
  23385.     @grantee        sysname = null
  23386.     )
  23387. as
  23388.     select
  23389.         GRANTOR     = user_name(p.grantor),
  23390.         GRANTEE     = user_name(u.uid),
  23391.         TABLE_CATALOG   = db_name(),
  23392.         TABLE_SCHEMA    = user_name(o.uid),
  23393.         TABLE_NAME  = o.name,
  23394.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  23395.                     case p.action
  23396.                     when 193 then N'SELECT'
  23397.                     when 195 then N'INSERT'
  23398.                     when 196 then N'DELETE'
  23399.                     when 197 then N'UPDATE'
  23400.                     else N'REFERENCES'
  23401.                     end),
  23402.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  23403.     from 
  23404.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  23405.     where
  23406.         o.name = @table_name
  23407.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23408.     and (@grantee is null or @grantee = user_name(u.uid))
  23409.     and     o.type in ('U','V','S')
  23410.     and p.id = o.id
  23411.     and     (@grantor is null or @grantor = user_name(p.grantor))
  23412.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  23413.     and     (u.uid > 0 and u.uid < 16384)
  23414.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  23415.     and     p.protecttype <> 206    /* only grant rows */
  23416.     and     p.action in (26,193,195,196,197)
  23417.     and     o.uid <> u.uid          /* no rows for owner */
  23418.     and     not exists (            /* exclude revoke'd privileges */
  23419.             select  *
  23420.             from    sysprotects p1
  23421.             where   p1.protecttype = 206
  23422.             and     p1.action = p.action
  23423.             and     p1.id = p.id
  23424.             and     p1.uid = u.uid)
  23425.     union
  23426.     select  /*  Add rows for table owner */
  23427.         GRANTOR     = user_name(u.uid),
  23428.         GRANTEE     = user_name(o.uid),
  23429.         TABLE_CATALOG   = db_name(),
  23430.         TABLE_SCHEMA    = user_name(o.uid),
  23431.         TABLE_NAME  = o.name,
  23432.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  23433.                     case v.number
  23434.                     when 193 then N'SELECT'
  23435.                     when 195 then N'INSERT'
  23436.                     when 196 then N'DELETE'
  23437.                     when 197 then N'UPDATE'
  23438.                     else N'REFERENCES'
  23439.                     end),
  23440.         IS_GRANTABLE    = convert(bit,1)    
  23441.     from 
  23442.         sysobjects o, master.dbo.spt_values v, sysusers u
  23443.     where
  23444.         o.name = @table_name
  23445.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23446.     and (@grantee is null or @grantee = user_name(o.uid))
  23447.     and     o.type in ('U','V','S')
  23448.     and     u.uid = 1       /* grantor is dbo of database */
  23449.     and (@grantor is null or @grantor = user_name(u.uid))
  23450.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23451.     and     v.number in (26,193,195,196,197)
  23452.     and     not exists (    /* exclude revoke'd privileges */
  23453.             select  *
  23454.             from    sysprotects p1
  23455.             where   p1.protecttype = 206
  23456.             and     p1.action = v.number
  23457.             and     p1.id = o.id
  23458.             and     p1.uid = o.uid)
  23459.     order by 4,5,6,1,2
  23460. go
  23461. dump tran master with no_log
  23462. go
  23463. CREATE PROCEDURE sp_table_privileges_rowset;2
  23464.     (
  23465.     @table_schema       sysname = null,
  23466.     @grantor        sysname = null,
  23467.     @grantee        sysname = null
  23468.     )
  23469. as
  23470.     select
  23471.         GRANTOR     = user_name(p.grantor),
  23472.         GRANTEE     = user_name(u.uid),
  23473.         TABLE_CATALOG   = db_name(),
  23474.         TABLE_SCHEMA    = user_name(o.uid),
  23475.         TABLE_NAME  = o.name,
  23476.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  23477.                     case p.action
  23478.                     when 193 then N'SELECT'
  23479.                     when 195 then N'INSERT'
  23480.                     when 196 then N'DELETE'
  23481.                     when 197 then N'UPDATE'
  23482.                     else N'REFERENCES'
  23483.                     end),
  23484.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  23485.     from 
  23486.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  23487.     where
  23488.         o.type in ('U','V','S')
  23489.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23490.     and (@grantee is null or @grantee = user_name(u.uid))
  23491.     and p.id = o.id
  23492.     and     (@grantor is null or @grantor = user_name(p.grantor))
  23493.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  23494.     and     (u.uid > 0 and u.uid < 16384)
  23495.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  23496.     and     p.protecttype <> 206    /* only grant rows */
  23497.     and     p.action in (26,193,195,196,197)
  23498.     and     o.uid <> u.uid          /* no rows for owner */
  23499.     and     not exists (            /* exclude revoke'd privileges */
  23500.             select  *
  23501.             from    sysprotects p1
  23502.             where   p1.protecttype = 206
  23503.             and     p1.action = p.action
  23504.             and     p1.id = p.id
  23505.             and     p1.uid = u.uid)
  23506.     union
  23507.     select  /*  Add rows for table owner */
  23508.         GRANTOR     = user_name(u.uid),
  23509.         GRANTEE     = user_name(o.uid),
  23510.         TABLE_CATALOG   = db_name(),
  23511.         TABLE_SCHEMA    = user_name(o.uid),
  23512.         TABLE_NAME  = o.name,
  23513.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  23514.                     case v.number
  23515.                     when 193 then N'SELECT'
  23516.                     when 195 then N'INSERT'
  23517.                     when 196 then N'DELETE'
  23518.                     when 197 then N'UPDATE'
  23519.                     else N'REFERENCES'
  23520.                     end),
  23521.         IS_GRANTABLE    = convert(bit,1)    
  23522.     from 
  23523.         sysobjects o, master.dbo.spt_values v, sysusers u
  23524.     where
  23525.         o.type in ('U','V','S')
  23526.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23527.     and (@grantee is null or @grantee = user_name(o.uid))
  23528.     and     u.uid = 1       /* grantor is dbo of database */
  23529.     and (@grantor is null or @grantor = user_name(u.uid))
  23530.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23531.     and     v.number in (26,193,195,196,197)
  23532.     and     not exists (    /* exclude revoke'd privileges */
  23533.             select  *
  23534.             from    sysprotects p1
  23535.             where   p1.protecttype = 206
  23536.             and     p1.action = v.number
  23537.             and     p1.id = o.id
  23538.             and     p1.uid = o.uid)
  23539.     order by 4,5,6,1,2
  23540. go
  23541. dump tran master with no_log
  23542. go
  23543. create procedure sp_table_privileges_rowset;5
  23544.     (
  23545.     @table_server       sysname,
  23546.     @table_catalog      sysname = null,
  23547.     @table_name     sysname = null,
  23548.     @table_schema       sysname = null,
  23549.     @grantor        sysname = null,
  23550.     @grantee        sysname = null
  23551.     )
  23552. as
  23553.     select
  23554.         GRANTOR,        
  23555.         GRANTEE,        
  23556.         TABLE_CATALOG,  
  23557.         TABLE_SCHEMA,   
  23558.         TABLE_NAME, 
  23559.         PRIVILEGE_TYPE, 
  23560.         IS_GRANTABLE    
  23561.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  23562.                 @table_server,
  23563.                 @table_catalog,
  23564.                 @table_schema,
  23565.                 @table_name,
  23566.                 @grantor,
  23567.                 @grantee >
  23568.     order by 3,4,5,6,1,2
  23569. go
  23570.  
  23571. grant execute on sp_table_privileges_rowset to public
  23572. go
  23573. dump tran master with no_log
  23574. go
  23575. if (charindex('6.00', @@version) > 0)
  23576.     begin
  23577.     if (exists (select * from sysobjects
  23578.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  23579.         begin
  23580.         drop proc sp_table_privileges_rowset
  23581.         dump tran master with no_log
  23582.         end
  23583.     end
  23584. go
  23585.  
  23586.  
  23587. print ''
  23588. print 'creating sp_linkedservers_rowset'
  23589. go
  23590. if (charindex('7.00', @@version) = 0 and
  23591.     charindex('8.00', @@version) = 0)
  23592. begin
  23593.     print ''
  23594.     print ''
  23595.     print 'Warning:'
  23596.     print 'you are installing the stored procedures '
  23597.     print 'on a pre 8.0 SQL Server.'
  23598.     print 'Ignore the following errors.'
  23599. end
  23600. go
  23601. /*  Procedure for 8.0 server */
  23602. create proc sp_linkedservers_rowset
  23603.     (
  23604.     @srvname    sysname
  23605.     )
  23606. as
  23607.     select
  23608.         SVR_NAME        = srvname,
  23609.         SVR_PRODUCT     = srvproduct,
  23610.         SVR_PROVIDERNAME    = providername,
  23611.         SVR_DATASOURCE      = datasource,
  23612.         SVR_PROVIDERSTRING  = providerstring,
  23613.         SVR_LOCATION        = location,
  23614.         SVR_CATALOG     = catalog
  23615.     from master.dbo.sysservers
  23616.     where srvname = @srvname and (srvstatus & 128) = 128
  23617.     order by 1
  23618. go
  23619. dump tran master with no_log
  23620. go
  23621. create proc sp_linkedservers_rowset;2
  23622. as
  23623.     select
  23624.         SVR_NAME        = srvname,
  23625.         SVR_PRODUCT     = srvproduct,
  23626.         SVR_PROVIDERNAME    = providername,
  23627.         SVR_DATASOURCE      = datasource,
  23628.         SVR_PROVIDERSTRING  = providerstring,
  23629.         SVR_LOCATION        = location,
  23630.         SVR_CATALOG     = catalog
  23631.     from master.dbo.sysservers
  23632.     where (srvstatus & 128) = 128
  23633.     order by 1
  23634. go
  23635.  
  23636. grant execute on sp_linkedservers_rowset to public
  23637. go
  23638.  
  23639. dump tran master with no_log
  23640. go
  23641.  
  23642. print ''
  23643. print 'creating sp_table_statistics_rowset'
  23644. go
  23645. if (charindex('8.00', @@version) = 0)
  23646. begin
  23647.     print ''
  23648.     print ''
  23649.     print 'Warning:'
  23650.     print 'you are installing the stored procedures '
  23651.     print 'on a pre 8.00 SQL Server.'
  23652.     print 'Ignore the following errors.'
  23653. end
  23654. go
  23655. /*  Procedure for 8.00 server */
  23656. create proc sp_table_statistics_rowset
  23657. as
  23658.     select
  23659.         TABLE_CATALOG = convert(sysname, null),
  23660.         TABLE_SCHEMA = convert(sysname, null),
  23661.         TABLE_NAME = convert(sysname, null),
  23662.         STATISTICS_CATALOG = convert(sysname, null),
  23663.         STATISTICS_SCHEMA = convert(sysname, null),
  23664.         STATISTICS_NAME = convert(sysname, null),
  23665.         STATISTICS_TYPE = convert(smallint,0),
  23666.         COLUMN_NAME = convert(sysname, null),
  23667.         COLUMN_GUID = convert(uniqueidentifier, null),
  23668.         COLUMN_PROPID = convert(int, null),
  23669.         ORDINAL_POSITION = convert(int, null),
  23670.         SAMPLE_PCT = convert(smallint, null),
  23671.         LAST_UPDATE_TIME = convert(datetime, null),
  23672.         NO_OF_RANGES = convert(int, null),
  23673.         COLUMN_CARDINALITY = convert(bigint, null),
  23674.         TUPLE_CARDINALITY = convert(bigint, null),
  23675.         TABLE_CARDINALITY = convert(bigint, null),
  23676.         AVG_COLUMN_LENGTH = convert(int, null)
  23677.     where 1=0
  23678. go
  23679. dump tran master with no_log
  23680. go
  23681.  
  23682. create proc sp_table_statistics_rowset;2
  23683.                 (@table_catalog     sysname = null,
  23684.                 @table_schema       sysname = null,
  23685.                 @table_name     sysname = null,
  23686.                 @stat_catalog       sysname = null,
  23687.                 @stat_schema        sysname = null,
  23688.                 @stat_name      sysname = null)
  23689. as 
  23690. begin
  23691.     
  23692.     set nocount on
  23693.  
  23694.     -- check in parameters
  23695.     if ((@table_catalog is not null) and (db_name() <> @table_catalog)
  23696.         or (@stat_catalog is not null) and (db_name() <> @stat_catalog))
  23697.         begin   /* If qualifier doesn't match current database */
  23698.             raiserror 20001 '~~Rush_5~~'
  23699.             return
  23700.         end
  23701.  
  23702.     --- create temp table
  23703.     create table #spstattab(
  23704.         tblcatalog  sysname collate database_default not null,
  23705.         tblschema   sysname collate database_default not null,
  23706.         tblname     sysname collate database_default not null,
  23707.         statname    sysname collate database_default not null
  23708.         )
  23709.  
  23710.     insert into #spstattab
  23711.     select 
  23712.         db_name(),
  23713.         user_name(o.uid),
  23714.         o.name,
  23715.         x.name
  23716.     from sysobjects o, sysindexes x
  23717.     where o.type in ('U')
  23718.         and (@table_name is null or o.name = @table_name)
  23719.         and (@table_schema is null or @table_schema = user_name(o.uid))
  23720.         and (@stat_name is null or @stat_name = x.name)
  23721.         and (@stat_schema is null or @stat_schema = user_name(o.uid))
  23722.         and x.id = o.id
  23723.         and permissions(o.id) <> 0
  23724.         and x.statblob is not null
  23725.     order by 1,2,3,4
  23726.  
  23727.     -- fast forward CURSOR OVER THE temp TABLE
  23728.     declare ff_csr  cursor fast_forward for
  23729.     select tblcatalog, tblschema, tblname, statname from #spstattab
  23730.  
  23731.     declare @tblcatalog sysname,
  23732.         @tblschema  sysname,
  23733.         @tblname    sysname,
  23734.         @statname   sysname,
  23735.         @qtbl       nvarchar(4000),
  23736.         @rowsetcount    int
  23737.  
  23738.     set @rowsetcount = 0
  23739.  
  23740.     open ff_csr
  23741.     fetch ff_csr into @tblcatalog, @tblschema, @tblname, @statname
  23742.     while @@fetch_status >= 0
  23743.     begin
  23744.         set @rowsetcount = @rowsetcount + 1
  23745.         set @qtbl = @tblcatalog + '.' + @tblschema + '.' + @tblname
  23746.         dbcc show_statistics(@qtbl, @statname) with stat_header join density_vector
  23747.         fetch ff_csr into @tblcatalog, @tblschema, @tblname, @statname
  23748.     end
  23749.     close ff_csr
  23750.     deallocate ff_csr
  23751.     
  23752.     --- drop temp table
  23753.     drop table #spstattab
  23754.  
  23755.     if @rowsetcount = 0
  23756.     begin
  23757.         select  
  23758.             TABLE_CATALOG = convert(sysname, null),
  23759.             TABLE_SCHEMA = convert(sysname, null),
  23760.             TABLE_NAME = convert(sysname, null),
  23761.             STATISTICS_CATALOG = convert(sysname, null),
  23762.             STATISTICS_SCHEMA = convert(sysname, null),
  23763.             STATISTICS_NAME = convert(sysname, null),
  23764.             STATISTICS_TYPE = convert(smallint,0),
  23765.             COLUMN_NAME = convert(sysname, null),
  23766.             COLUMN_GUID = convert(uniqueidentifier, null),
  23767.             COLUMN_PROPID = convert(int, null),
  23768.             ORDINAL_POSITION = convert(int, null),
  23769.             SAMPLE_PCT = convert(smallint, null),
  23770.             LAST_UPDATE_TIME = convert(datetime, null),
  23771.             NO_OF_RANGES = convert(int, null),
  23772.             COLUMN_CARDINALITY = convert(bigint, null),
  23773.             TUPLE_CARDINALITY = convert(bigint, null),
  23774.             TABLE_CARDINALITY = convert(bigint, null),
  23775.             AVG_COLUMN_LENGTH = convert(int, null)
  23776.         where 1=0
  23777.     end
  23778.  
  23779.     set nocount off
  23780. end
  23781. go
  23782.  
  23783. grant execute on sp_table_statistics_rowset to public
  23784. go
  23785.  
  23786. dump tran master with no_log
  23787. go
  23788.  
  23789. print ''
  23790. print 'creating sp_oledb_column_constraints'
  23791. go
  23792.  
  23793. /* Procedure for 6.5 server */
  23794. create procedure sp_oledb_column_constraints
  23795.     (@tblname varchar(225)      -- the table to check for constraints
  23796.     ,@colname varchar(225)      -- the column to check for constraints
  23797.     ,@dropcnst smallint)
  23798. as
  23799. begin
  23800.     -- PRELIM
  23801.     set nocount on
  23802.  
  23803.     declare @tblid      int           -- the object id of the table
  23804.         ,@cnstname      varchar(225)   -- name of const. currently under consideration
  23805.         ,@cnstid        int
  23806.         ,@cnstatus      int
  23807.         ,@dbname        varchar(30)
  23808.  
  23809.     -- Create temp table
  23810.     create table #spcnsttab
  23811.     (cnst_name      varchar(225)        NOT NULL)
  23812.  
  23813.     -- Check to see that the object names are local to the current database.
  23814.     if @tblname like '%.%.%'
  23815.        and  substring(@tblname, 1, charindex('.', @tblname) - 1) <> db_name()
  23816.     begin
  23817.         raiserror(15250,-1,-1)
  23818.         return (1)
  23819.     end
  23820.     -- Check to see if the table exists and initialize @objid.
  23821.     select @tblid = object_id(@tblname)
  23822.     if @tblid is NULL
  23823.     begin
  23824.         select @dbname=db_name()
  23825.         raiserror(15009,-1,-1,@tblname,@dbname)
  23826.         return (1)
  23827.     end
  23828.  
  23829.     -- STATIC CURSOR OVER THE TABLE'S CONSTRAINTS
  23830.     declare cnst_csr insensitive cursor for
  23831.         select c.constid, c.status, o.name 
  23832.         from sysconstraints c, sysobjects o 
  23833.         where c.id = @tblid and o.id = c.constid
  23834.             and ((c.status & 0xf)=1 and (@dropcnst & 16)<> 0
  23835.             or (c.status & 0xf)=2 and (@dropcnst & 32)<> 0 
  23836.             or (c.status & 0xf)=5 and (@dropcnst & 2)<> 0)  
  23837.         -- ONLY 6.5 sysconstraints objects
  23838.         for read only
  23839.  
  23840.     -- Now check out each constraint, figure out its type and keys and
  23841.     -- save the info in a temporary table that we'll print out at the end.
  23842.     open cnst_csr
  23843.     fetch cnst_csr into @cnstid, @cnstatus, @cnstname
  23844.     while @@fetch_status >= 0
  23845.     begin
  23846.         if ((@cnstatus & 0xf) in (1, 2)) ---- primary key, unique
  23847.             begin
  23848.                 -- get indid
  23849.                 declare @indid smallint
  23850.                 select  @indid = indid
  23851.                 from    sysindexes
  23852.                 where   name = object_name(@cnstid) and id = @tblid
  23853.  
  23854.                 if (@colname = index_col(@tblname, @indid, 1)) 
  23855.                     and (index_col(@tblname, @indid, 2) is null)
  23856.                     begin
  23857.                         -- ADD TO TABLE
  23858.                         insert into #spcnsttab
  23859.                         values (@cnstname)
  23860.                     end
  23861.             end
  23862.         else
  23863.         if (@cnstatus & 0xf) = 5
  23864.             begin
  23865.                 if (select col_name(id, colid) from sysconstraints where colid > 0 and constid=@cnstid) = @colname
  23866.                     begin
  23867.                         insert into #spcnsttab
  23868.                         values (@cnstname)
  23869.                     end
  23870.             end
  23871.  
  23872.         fetch cnst_csr into @cnstid ,@cnstatus ,@cnstname
  23873.     end     --of major loop
  23874.     close cnst_csr
  23875.     deallocate cnst_csr
  23876.  
  23877.     -- Now print out the contents of the temporary index table.
  23878.     select 'constraint_name' = cnst_name
  23879.     from #spcnsttab 
  23880.     
  23881.     drop table #spcnsttab
  23882.  
  23883.     set nocount off
  23884.     return (0)
  23885. end 
  23886. go
  23887. dump tran master with no_log
  23888. go
  23889.  
  23890. if (charindex('7.00', @@version) = 0 and
  23891.     charindex('8.00', @@version) = 0)
  23892. begin
  23893.     print ''
  23894.     print ''
  23895.     print 'Warning:'
  23896.     print 'you are installing the stored procedures '
  23897.     print 'on a pre 7.0 SQL Server.'
  23898.     print 'Ignore the following errors.'
  23899. end
  23900. else
  23901.     drop proc sp_oledb_column_constraints
  23902. go
  23903.  
  23904. /* Procedure for 7.0 server */
  23905. create procedure sp_oledb_column_constraints
  23906.     (@tblname nvarchar(776)     -- the table to check for constraints
  23907.     ,@colname nvarchar(776)     -- the column to check for constraints
  23908.     ,@dropcnst smallint)
  23909. as
  23910. begin
  23911.     -- PRELIM
  23912.     set nocount on
  23913.  
  23914.     declare @tblid      int           -- the object id of the table
  23915.         ,@cnstname      sysname       -- name of const. currently under consideration
  23916.         ,@cnstid        int
  23917.         ,@cnsttype      character(2)
  23918.         ,@dbname        sysname
  23919.  
  23920.     -- Create temp table
  23921.     create table #spcnsttab
  23922.     (cnst_name      sysname     NOT NULL)
  23923.  
  23924.     -- Check to see that the object names are local to the current database.
  23925.     select @dbname = parsename(@tblname,3) 
  23926.     if @dbname is not null and @dbname <> db_name()
  23927.     begin
  23928.         raiserror(15250,-1,-1)
  23929.         return (1)
  23930.     end
  23931.  
  23932.     -- Check to see if the table exists and initialize @objid.
  23933.     select @tblid = object_id(@tblname)
  23934.     if @tblid is NULL
  23935.     begin
  23936.         select @dbname=db_name()
  23937.         raiserror(15009,-1,-1,@tblname,@dbname)
  23938.         return (1)
  23939.     end
  23940.  
  23941.     -- STATIC CURSOR OVER THE TABLE'S CONSTRAINTS
  23942.     declare cnst_csr cursor fast_forward for
  23943.         select id, xtype, name 
  23944.         from sysobjects 
  23945.         where parent_obj = @tblid
  23946.             and ((xtype = 'PK' and (@dropcnst & 16)<> 0) 
  23947.             or (xtype = 'UQ' and (@dropcnst & 32)<> 0)
  23948.             or (xtype = 'D ' and (@dropcnst & 2)<> 0))  
  23949.         -- ONLY 6.5 sysconstraints objects
  23950.         for read only
  23951.  
  23952.     -- Now check out each constraint, figure out its type and keys and
  23953.     -- save the info in a temporary table that we'll print out at the end.
  23954.     open cnst_csr
  23955.     fetch cnst_csr into @cnstid, @cnsttype, @cnstname
  23956.     while @@fetch_status >= 0
  23957.     begin
  23958.         if @cnsttype in ('PK','UQ')
  23959.             begin
  23960.                 -- get indid
  23961.                 declare @indid smallint
  23962.                 select  @indid = indid
  23963.                 from    sysindexes
  23964.                 where   name = object_name(@cnstid) and id = @tblid
  23965.  
  23966.                 if (@colname = index_col(@tblname, @indid, 1)) 
  23967.                     and (index_col(@tblname, @indid, 2) is null)
  23968.                     begin
  23969.                         -- ADD TO TABLE
  23970.                         insert into #spcnsttab
  23971.                         values (@cnstname)
  23972.                     end
  23973.             end
  23974.         else
  23975.         if (@cnsttype = 'D ')
  23976.             begin
  23977.                 if (select col_name(@tblid, info) from sysobjects where id=@cnstid) = @colname
  23978.                     begin
  23979.                         insert into #spcnsttab
  23980.                         values (@cnstname)
  23981.                     end
  23982.             end
  23983.  
  23984.         fetch cnst_csr into @cnstid ,@cnsttype ,@cnstname
  23985.     end     --of major loop
  23986.     close cnst_csr
  23987.     deallocate cnst_csr
  23988.  
  23989.     -- Now print out the contents of the temporary index table.
  23990.     select 'constraint_name' = cnst_name
  23991.     from #spcnsttab 
  23992.  
  23993.     drop table #spcnsttab
  23994.     
  23995.     set nocount off
  23996.     return (0)
  23997. end
  23998. go
  23999.  
  24000. if (charindex('8.00', @@version) = 0)
  24001. begin
  24002.     print ''
  24003.     print ''
  24004.     print 'Warning:'
  24005.     print 'you are installing the stored procedures '
  24006.     print 'on a pre 8.0 SQL Server.'
  24007.     print 'Ignore the following errors.'
  24008. end
  24009. else
  24010.     drop proc sp_oledb_column_constraints
  24011. go
  24012.  
  24013. /* Procedure for 8.0 server */
  24014. create procedure sp_oledb_column_constraints
  24015.     (@tblname nvarchar(776)     -- the table to check for constraints
  24016.     ,@colname nvarchar(776)     -- the column to check for constraints
  24017.     ,@dropcnst smallint)
  24018. as
  24019. begin
  24020.     -- PRELIM
  24021.     set nocount on
  24022.  
  24023.     declare @tblid      int           -- the object id of the table
  24024.         ,@cnstname      sysname       -- name of const. currently under consideration
  24025.         ,@cnstid        int
  24026.         ,@cnsttype      character(2)
  24027.         ,@dbname        sysname
  24028.  
  24029.     -- Create temp table
  24030.     create table #spcnsttab
  24031.     (cnst_name      sysname     collate database_default NOT NULL)
  24032.  
  24033.     -- Check to see that the object names are local to the current database.
  24034.     select @dbname = parsename(@tblname,3) 
  24035.     if @dbname is not null and @dbname <> db_name()
  24036.     begin
  24037.         raiserror(15250,-1,-1)
  24038.         return (1)
  24039.     end
  24040.  
  24041.     -- Check to see if the table exists and initialize @objid.
  24042.     select @tblid = object_id(@tblname)
  24043.     if @tblid is NULL
  24044.     begin
  24045.         select @dbname=db_name()
  24046.         raiserror(15009,-1,-1,@tblname,@dbname)
  24047.         return (1)
  24048.     end
  24049.  
  24050.     -- STATIC CURSOR OVER THE TABLE'S CONSTRAINTS
  24051.     declare cnst_csr cursor fast_forward for
  24052.         select id, xtype, name 
  24053.         from sysobjects 
  24054.         where parent_obj = @tblid
  24055.             and ((xtype = 'PK' and (@dropcnst & 16)<> 0) 
  24056.             or (xtype = 'UQ' and (@dropcnst & 32)<> 0)
  24057.             or (xtype = 'D ' and (@dropcnst & 2)<> 0))  
  24058.         -- ONLY 6.5 sysconstraints objects
  24059.         for read only
  24060.  
  24061.     -- Now check out each constraint, figure out its type and keys and
  24062.     -- save the info in a temporary table that we'll print out at the end.
  24063.     open cnst_csr
  24064.     fetch cnst_csr into @cnstid, @cnsttype, @cnstname
  24065.     while @@fetch_status >= 0
  24066.     begin
  24067.         if @cnsttype in ('PK','UQ')
  24068.             begin
  24069.                 -- get indid
  24070.                 declare @indid smallint
  24071.                 select  @indid = indid
  24072.                 from    sysindexes
  24073.                 where   name = object_name(@cnstid) and id = @tblid
  24074.  
  24075.                 if (@colname = index_col(@tblname, @indid, 1)) 
  24076.                     and (index_col(@tblname, @indid, 2) is null)
  24077.                     begin
  24078.                         -- ADD TO TABLE
  24079.                         insert into #spcnsttab
  24080.                         values (@cnstname)
  24081.                     end
  24082.             end
  24083.         else
  24084.         if (@cnsttype = 'D ')
  24085.             begin
  24086.                 if (select col_name(@tblid, info) from sysobjects where id=@cnstid) = @colname
  24087.                     begin
  24088.                         insert into #spcnsttab
  24089.                         values (@cnstname)
  24090.                     end
  24091.             end
  24092.  
  24093.         fetch cnst_csr into @cnstid ,@cnsttype ,@cnstname
  24094.     end     --of major loop
  24095.     close cnst_csr
  24096.     deallocate cnst_csr
  24097.  
  24098.     -- Now print out the contents of the temporary index table.
  24099.     select 'constraint_name' = cnst_name
  24100.     from #spcnsttab 
  24101.  
  24102.     drop table #spcnsttab
  24103.     
  24104.     set nocount off
  24105.     return (0)
  24106. end
  24107. go
  24108.  
  24109. grant execute on sp_oledb_column_constraints to public
  24110. go
  24111.  
  24112. dump tran master with no_log
  24113. go
  24114.  
  24115. print ''
  24116. print 'creating sp_oledb_indexinfo'
  24117. go
  24118.  
  24119. /* Procedure for 6.5 server */
  24120. create procedure sp_oledb_indexinfo
  24121.         @objname varchar(225)       -- the table to check for indexes
  24122.         ,@indname varchar(225)
  24123. as
  24124. begin
  24125.     -- PRELIM
  24126.     set nocount on
  24127.  
  24128.     declare @objid int,         -- the object id of the table
  24129.         @indid smallint,    -- the index id of an index
  24130.         @status int,
  24131.         @dbname varchar(30),
  24132.         @OrigFillFactor tinyint,
  24133.         @i int, 
  24134.         @thiskey varchar(32),
  24135.         @tptr     varbinary(16) -- pointer for building text strings.
  24136.     ---- Check to see that the object names are local to the current database.
  24137.     if @objname like '%.%.%'
  24138.        and  substring(@objname, 1, charindex('.', @objname) - 1) <> db_name()
  24139.     begin
  24140.         raiserror(15250,-1,-1)
  24141.         return (1)
  24142.     end
  24143.     ---- Check to see if the table exists and initialize @objid.
  24144.     select @objid = object_id(@objname)
  24145.     ---- Table does not exist so return.
  24146.     if @objid is NULL
  24147.     begin
  24148.         select @dbname=db_name()
  24149.         raiserror(15009,-1,-1,@objname,@dbname)
  24150.         return (1)
  24151.     end
  24152.  
  24153.     select @indid = indid, @status = status, @OrigFillFactor = OrigFillFactor
  24154.     from sysindexes
  24155.     where id = @objid and name = @indname 
  24156.  
  24157.     -- IF NO INDEX, QUIT
  24158.     if @indid is NULL
  24159.     begin
  24160.         raiserror(15472,-1,-1) --'Object does not have any indexes.'
  24161.         return (1)
  24162.     end
  24163.     -- create temp table
  24164.     create table #spindtab
  24165.     (
  24166.         status              int,
  24167.         OrigFillFactor          tinyint,
  24168.         index_keys          text    NOT NULL,
  24169.     )
  24170.     -- Now check out each index, figure out its type and keys and
  24171.     -- First we'll figure out what the keys are.
  24172.     select @i = 1
  24173.     while (@i <= 16)
  24174.         begin
  24175.             select @thiskey = index_col(@objname, @indid, @i)
  24176.             if @thiskey is NULL
  24177.                     goto keysdone
  24178.             if @i=1
  24179.                 begin
  24180.                     insert into #spindtab
  24181.                     values (@status, @OrigFillFactor, @thiskey)
  24182.  
  24183.             select @tptr = textptr(index_keys) from #spindtab
  24184.                 end
  24185.             else
  24186.                 begin
  24187.                     select @thiskey = ', ' + @thiskey
  24188.  
  24189.             if @tptr is not null
  24190.                     updatetext #spindtab.index_keys @tptr null null @thiskey
  24191.                 end
  24192.  
  24193.         select @i = @i + 1
  24194.          end --loop 16
  24195.          ---- When we get here we now have all the keys.
  24196.  
  24197. keysdone:
  24198.     select * from #spindtab
  24199.     
  24200.     drop table #spindtab
  24201.     set nocount off
  24202.     return (0)
  24203. end
  24204. go
  24205. dump tran master with no_log
  24206. go
  24207.  
  24208. if (charindex('7.00', @@version) = 0 and
  24209.     charindex('8.00', @@version) = 0)
  24210. begin
  24211.     print ''
  24212.     print ''
  24213.     print 'Warning:'
  24214.     print 'you are installing the stored procedures '
  24215.     print 'on a pre 8.0 SQL Server.'
  24216.     print 'Ignore the following errors.'
  24217. end
  24218. else
  24219.     drop proc sp_oledb_indexinfo
  24220. go
  24221.  
  24222. /* Procedure for 8.0 server */
  24223. create proc sp_oledb_indexinfo
  24224.         @objname nvarchar(776)      -- the table to check for indexes
  24225.         ,@indname nvarchar(776)
  24226. as
  24227. begin
  24228.     -- PRELIM
  24229.     set nocount on
  24230.  
  24231.     declare @objid int,         -- the object id of the table
  24232.         @indid smallint,    -- the index id of an index
  24233.         @status int,
  24234.         @keys nvarchar(2078),-- string build index key list, length = (16*max_id_length)+(15*2)
  24235.         @dbname sysname,
  24236.         @OrigFillFactor tinyint
  24237.  
  24238.     -- Check to see that the object names are local to the current database.
  24239.     select @dbname = parsename(@objname,3) 
  24240.  
  24241.     if @dbname is not null and @dbname <> db_name()
  24242.     begin
  24243.             raiserror(15250,-1,-1)
  24244.             return (1)
  24245.     end
  24246.  
  24247.     -- Check to see the the table exists and initialize @objid.
  24248.     select @objid = object_id(@objname)
  24249.     if @objid is NULL
  24250.     begin
  24251.         select @dbname=db_name()
  24252.         raiserror(15009,-1,-1,@objname,@dbname)
  24253.         return (1)
  24254.     end
  24255.  
  24256.     select @indid = indid, @status = status, @OrigFillFactor = OrigFillFactor
  24257.     from sysindexes
  24258.     where id = @objid and name = @indname 
  24259.  
  24260.     -- IF NO INDEX, QUIT
  24261.     if @indid is NULL
  24262.     begin
  24263.         raiserror(15472,-1,-1) --'Object does not have any indexes.'
  24264.         return (1)
  24265.     end
  24266.  
  24267.     -- First we'll figure out what the keys are.
  24268.     declare @i int, @thiskey sysname
  24269.     select @keys = index_col(@objname, @indid, 1),
  24270.         @i = 2, @thiskey = index_col(@objname, @indid, 2)
  24271.     while (@thiskey is not null )
  24272.     begin
  24273.         select @keys = @keys + ', ' + @thiskey, @i = @i + 1
  24274.         select @thiskey = index_col(@objname, @indid, @i)
  24275.     end
  24276.     -- DISPLAY THE RESULTS
  24277.     select 'Status'=@status, 'OrigFillFactor' =@OrigFillFactor, 'Index keys'=@keys
  24278.     set nocount off
  24279.     return (0) 
  24280. end
  24281. go
  24282.  
  24283. grant execute on sp_oledb_indexinfo to public
  24284. go
  24285.  
  24286. dump tran master with no_log
  24287. go
  24288.  
  24289. print ''
  24290. print 'creating sp_oledb_ro_usrname'
  24291. go
  24292.  
  24293. create procedure sp_oledb_ro_usrname
  24294. as
  24295. begin
  24296.     select substring('NY',status/1024&1+1,1),user_name() 
  24297.     from master..sysdatabases 
  24298.     where name=DB_NAME()
  24299. end
  24300. go
  24301.  
  24302. grant execute on sp_oledb_ro_usrname to public
  24303. go
  24304.  
  24305. dump tran master with no_log
  24306. go
  24307.  
  24308. if (charindex('6.00', @@version) > 0 or
  24309.     charindex('6.50', @@version) > 0 or
  24310.     charindex('7.00', @@version) > 0)
  24311.     begin
  24312.     if (exists (select * from sysobjects
  24313.         where name = 'sp_oledb_ro_usrname' and type = 'P '))
  24314.         begin
  24315.             drop proc sp_oledb_ro_usrname
  24316.             dump tran master with no_log
  24317.         end
  24318.     end
  24319. go
  24320.  
  24321. print ''
  24322. print 'creating sp_oledb_deflang'
  24323. go
  24324.  
  24325. if (charindex('8.00', @@version) = 0)
  24326. begin
  24327.     print ''
  24328.     print ''
  24329.     print 'Warning:'
  24330.     print 'you are installing the stored procedures '
  24331.     print 'on a pre 8.0 SQL Server.'
  24332.     print 'Ignore the following errors.'
  24333. end
  24334. go
  24335.  
  24336. create procedure sp_oledb_deflang
  24337. as
  24338.     begin
  24339.         select ISNULL(language,'us_english') 
  24340.         from master..syslogins 
  24341.         where sid=SUSER_SID()
  24342.     end
  24343. go
  24344.  
  24345. grant execute on sp_oledb_deflang to public
  24346. go
  24347.  
  24348. dump tran master with no_log
  24349. go
  24350.  
  24351. if (charindex('6.00', @@version) > 0 or
  24352.     charindex('6.50', @@version) > 0 or
  24353.     charindex('7.00', @@version) > 0)
  24354.     begin
  24355.     if (exists (select * from sysobjects
  24356.         where name = 'sp_oledb_deflang' and type = 'P '))
  24357.         begin
  24358.             drop proc sp_oledb_deflang
  24359.             dump tran master with no_log
  24360.         end
  24361.     end
  24362. go
  24363.  
  24364. print ''
  24365. print 'creating sp_oledb_defdb'
  24366. go
  24367.  
  24368. if (charindex('8.00', @@version) = 0)
  24369. begin
  24370.     print ''
  24371.     print ''
  24372.     print 'Warning:'
  24373.     print 'you are installing the stored procedures '
  24374.     print 'on a pre 8.0 SQL Server.'
  24375.     print 'Ignore the following errors.'
  24376. end
  24377. go
  24378.  
  24379. create procedure sp_oledb_defdb
  24380. as
  24381.     begin
  24382.         select dbname from master..syslogins where sid=SUSER_SID()
  24383.     end
  24384. go
  24385.  
  24386. grant execute on sp_oledb_defdb to public
  24387. go
  24388.  
  24389. dump tran master with no_log
  24390. go
  24391.  
  24392. if (charindex('6.00', @@version) > 0 or
  24393.     charindex('6.50', @@version) > 0 or
  24394.     charindex('7.00', @@version) > 0)
  24395.     begin
  24396.     if (exists (select * from sysobjects
  24397.         where name = 'sp_oledb_defdb' and type = 'P '))
  24398.         begin
  24399.             drop proc sp_oledb_defdb
  24400.             dump tran master with no_log
  24401.         end
  24402.     end
  24403. go
  24404.  
  24405. print ''
  24406. print 'creating sp_oledb_database'
  24407. go
  24408.  
  24409. create procedure sp_oledb_database
  24410. as
  24411.     begin
  24412.         select name from master..sysdatabases
  24413.     end
  24414. go
  24415.  
  24416. grant execute on sp_oledb_database to public
  24417. go
  24418.  
  24419. dump tran master with no_log
  24420. go
  24421.  
  24422. if (charindex('6.00', @@version) > 0 or
  24423.     charindex('6.50', @@version) > 0 or
  24424.     charindex('7.00', @@version) > 0)
  24425.     begin
  24426.     if (exists (select * from sysobjects
  24427.         where name = 'sp_oledb_database' and type = 'P '))
  24428.         begin
  24429.             drop proc sp_oledb_database
  24430.             dump tran master with no_log
  24431.         end
  24432.     end
  24433. go
  24434.  
  24435. print ''
  24436. print 'creating sp_oledb_language'
  24437. go
  24438.  
  24439. create procedure sp_oledb_language
  24440. as
  24441. begin
  24442.     select 'English','us_english' 
  24443.     union 
  24444.     select alias,name from master..syslanguages
  24445. end
  24446. go
  24447.  
  24448. grant execute on sp_oledb_language to public
  24449. go
  24450.  
  24451. dump tran master with no_log
  24452. go
  24453. if (charindex('6.00', @@version) > 0 or
  24454.     charindex('6.50', @@version) > 0 or
  24455.     charindex('7.00', @@version) > 0)
  24456.     begin
  24457.     if (exists (select * from sysobjects
  24458.         where name = 'sp_oledb_language' and type = 'P '))
  24459.         begin
  24460.             drop proc sp_oledb_language
  24461.             dump tran master with no_log
  24462.         end
  24463.     end
  24464. go
  24465.  
  24466. /*---------------------------- END OLEDB CATALOG PROCS ------------------------*/
  24467.  
  24468.  
  24469. /*---------------------------- BEGIN BCP CATALOG PROCS ------------------------*/
  24470.  
  24471. print 'creating sp_tablecollations'
  24472. go
  24473.  
  24474. /* Used by BCP to gather all the collation names for a table */
  24475. if (charindex('8.00', @@version) = 0)
  24476. begin
  24477.     print ''
  24478.     print ''
  24479.     print 'Warning:'
  24480.     print 'you are installing the stored procedures '
  24481.     print 'on a pre 8.0 SQL Server.'
  24482.     print 'Ignore the following errors.'
  24483. end
  24484. go
  24485.  
  24486. /* Procedure for 8.0 server */
  24487. create procedure sp_tablecollations (@object nvarchar(4000))
  24488. as
  24489. begin
  24490.     select c.colid,
  24491.            c.name, 
  24492.            tds_collation = c.tdscollation,
  24493.            collation_name = BCPCollationName(c.tdscollation, c.xtype)
  24494.     from syscolumns c inner join sysobjects t on c.id = t.id
  24495.     where t.id = object_id(@object, 'local')
  24496.     order by c.colid
  24497. end
  24498. go
  24499.  
  24500. grant execute on sp_tablecollations to public
  24501. go
  24502.  
  24503. print 'creating sp_bcp_dbcmptlevel'
  24504. go
  24505.  
  24506. /* Used by BCP to get current db compatibility level */
  24507. if (charindex('8.00', @@version) = 0)
  24508. begin
  24509.     print ''
  24510.     print ''
  24511.     print 'Warning:'
  24512.     print 'you are installing the stored procedures '
  24513.     print 'on a pre 8.0 SQL Server.'
  24514.     print 'Ignore the following errors.'
  24515. end
  24516. go
  24517.  
  24518. /* Procedure for 8.0 server */
  24519. create procedure sp_bcp_dbcmptlevel (@dbname sysname)
  24520. as
  24521. begin
  24522.     select cmptlevel
  24523.     from master.dbo.sysdatabases
  24524.     where name = @dbname
  24525. end
  24526. go
  24527.  
  24528. grant execute on sp_bcp_dbcmptlevel to public
  24529. go
  24530.  
  24531. /*---------------------------- END BCP CATALOG PROCS ------------------------*/
  24532.  
  24533. dump tran master with no_log
  24534. go
  24535.  
  24536.  
  24537.  
  24538.  
  24539. if (exists (select * from sysobjects
  24540.         where name = 'sp_configure' and type = 'P '))
  24541.     begin
  24542.         exec sp_configure 'allow updates',0
  24543.         reconfigure with override
  24544.     end
  24545. go
  24546.  
  24547. exec sp_MS_upd_sysobj_category 2 /* set category | 2 based on crdate. */
  24548.  
  24549. go
  24550.  
  24551. if exists (select * from sysobjects where name = 'sp_check_objects'
  24552.             and type = 'P ')
  24553.     begin
  24554.         /* Only supported on 6.0 servers */
  24555.         print ''
  24556.         print 'Checking objects created by instcat.sql.'
  24557.  
  24558.         exec sp_check_objects 'catalog'
  24559.     end
  24560. go
  24561.  
  24562. print ''
  24563. print 'instcat.sql completed successfully.'
  24564. go
  24565.  
  24566. set quoted_identifier off
  24567. go
  24568.  
  24569. dump tran master with no_log
  24570. go
  24571. checkpoint
  24572. go
  24573. /**/
  24574.